-1

I'm really new to java and i'm taking an introductory class to computer science. I need to know how to Prompt the user to user for two values, declare and define 2 variables to store the integers, and then be able to read the values in, and finally print the values out. But im pretty lost and i dont even know how to start i spent a whole day trying.. I really need some help/guidance. I need to do that for integers, decimal numbers and strings. Can someone help me?

3
  • Didn't they show you how to do this in class? Commented Oct 25, 2014 at 17:48
  • i missed the second day of lecture.... i looked on our online class but all i saw was an upcoming test :( Commented Oct 25, 2014 at 17:49
  • 1
    possible duplicate of reading int from console Commented Oct 25, 2014 at 17:55

3 Answers 3

2

You can do this by using Scanner class : A simple text scanner which can parse primitive types and strings using regular expressions. A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

For example, this code allows a user to read a number from System.in:

Scanner scan = new Scanner(System.in);
int i = scan.nextInt();
int j = scan.nextInt();
System.out.println("i = "+i +" j = "+j);

nextInt() : -Scans the next token of the input as an int and returns the int scanned from the input.

For more.

or to get user input you can also use the Console class : provides methods to access the character-based console device, if any, associated with the current Java virtual machine.

Console console = System.console();
String s = console.readLine();
int i = Integer.parseInt(console.readLine());

or you can also use BufferedReader and InputStreamReader classes and DataInputStream class to get user input .

Sign up to request clarification or add additional context in comments.

Comments

1

Use the Scanner class to get the values from the user. For integers you should use int, for decimal numbers (also called real numbers) use double and for strings use Strings.

A little example:

Scanner scan = new Scanner(System.in);
int intValue;
double decimalValue;
String textValue;

System.out.println("Please enter an integer value");
intValue = scan.nextInt(); // see how I use nextInt() for integers
System.out.println("Please enter a real number");
decimalValue = scan.nextDouble(); // nextDouble() for real numbers
System.out.println("Please enter a string value");
textValue = scan.next();    // next() for string variables

System.out.println("Your integer is: " + intValue + ", your real number is: "
                                + decimalValue + " and your string is: " + textValue);

If you still don't understand something, please look further into the Scanner class via google.

Comments

1

As you will likely continue to run into problems like this in your class and in your programming career:

Lessons on fishing.

  • Learn to explore the provided tutorials through oracle.
  • Learn to read the Java API documentation

Now to the fish.

You can use the Scanner class. Example provided in the documentation.

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.