Java programming... easy question

Hi, what i need to do is get input from the user, but i have no idea how. Lets just say i want to make a program like this:

"Please enter a number: " //user enters 3 for example
“You entered 3”

How would i go about doing that with java? Thanks in advance!

Something like:


BufferedReader r = new BufferedReader(new InputStreamReader(System.in));

String line = r.readLine();

if (line != null)
{
  // process it here
}

Well other than declaring variables, doing math, and displaying it, i know NO java. I literally came into class my first day and was told to make a program that i need input for without being taught one thing. I mean, i usually would learn well if im taught simply the basics, but it seems like the most important step was skipped in this damn class…

Oops forgot to put the main point into my last post… anyways, the point is that i dont understand what you mean by “process here”, and im also getting a compile error for “if (line != null)”

Thanks,
culov

Sounds like you need to grab a copy of “Learning Java for Dummies,” or something similar. Plus, I’m sure you can find free online resources to get you started as well.

They’re probably talking about taking an argument when the program runs. I don’t have the exact code (I’m aweful at writing code completely from scratch), but this might help.
part of java program
Public void main(String args)
{
for(int x=0;x<args.length();x++)
{
System.out.println("Command line argument entered: " + args);
}
}
To run:
java program_name argument1 argument2 etc

hth.

Here’s the Java “Bible” FWIW:

http://java.sun.com/docs/books/tutorial/

All right, because I’m nice, I will write something for you that does exactly what you asked, no more. Then I will explain it.

This, by the way, runs exactly as written.

// All code copyright elfbabe, 2003.

// You need the Java io libraries to do stuff with input
import java.io.*;

// class culov = in a file called culov.java
public class culov {

// Main method. Program starts running here.
// Throws IOException means that if something messed up
// happens with the input/output, you're not doing anything.

// must say this, or compiler will complain
public static void main(String args) throws IOException{

// You're creating a BufferedReader called "in" that
// will read input from the console, System.in.
BufferedReader in = new BufferedReader
    (new InputStreamReader(System.in));

// Display the stuff in quotes on the console
System.out.println("Please enter a number:");

// The user gets a blank line

// whatever they type is interpreted
// as an integer
int theInput =
Integer.parseInt(in.readLine());

// Display what’s in quotes
// Add theInput to the end
System.out.println("You entered " + theInput);
}
}

This will store whatever the user entered, number or not, as an Int. It’s dumb, though, so if the user enters something OTHER than an int, you’ll get a bunch of exceptions, and probably a crappy grade. At least I’d hope so. There are lots of ways of dealing with it, you can look them up. But this should get you started. If you want to store input as a String, it’s just String theInput = in.readLine();

Good luck.

Is this a Java class?? Or a class about something else, for which programming is needed?

It seems ridiculous to start a Java class by expecting students to write immediate programs that do input from standard input (on the console), since that is more complicated than standard input in other languages (like learning quick cin statements in C++ or scanf calls in C). Console output is easy from lesson 1 in Java, but console input not as easy for the first day. You have to know about some of the classes to use (Reader class and various subclasses, like BufferedReader) along with the methods in them.

I taught an intro Java course this summer. Here is a link to the first of the online notes outlines I created for my students. There are a few code examples there, including an example of a simple input class that provides functions for reading integers, floats, or strings.

Java Basics

How High’s the Water Mama? - Johnny Cash

Ball of Confusion - Temptations

Rhythm of the Rain - Cascades

Twisting the Night Away - Sam Cooke