Perry Smith

Java Questions & Answers

Home / Java Q & A Archive Page 1 / Java Q & A Archive / Java Q & A Main Page

8-25-98

I am just learning JAVA and have a very simple question. Is there a quick and easy way to accept an integer value from the terminal screen and store it in a variable name. I know it sounds ridiculous, but sometimes these very basic questions are never addressed in the tutorials.
thanks!

Tina


Responses
8-26-98

It's probably not addressed b/c it's not a very basic concept in Java ... you have to get into data streams and all ... the easiest way is to use the methods provided in some pre-written class... you can probably find a user input utility on the web with little difficulty. Or, I can give you some code for it, at your request.

Mike


8-26-98

import java.io*;
// Reads two integers and prints their sum
class Addition{

public static void main (String[] args) throws
IOException {
BufferedReader stdin = new BufferedReader
(new InputStreamReader(System.in));
String string1, string2;
int num1, num2, sum;

System.out.println ("Enter a number:");
string1 = stdin.readLine( );
num1 = Integer.parseInt (string1);

System.out.println ("Enter another number:");
string2 = stdin.readLine ( );
num2 = Integer.parseInt (string2);

sum = num1 + num2;
System.out.println("The sum is " + sum);
}// method main
}//class Adition

Let me know if this helps.

Perry Smith


8-26-98

Thank you very much! Your answers and code examples are most helpful.

Tina


Home / Java Q & A Archive Page 1 / Java Q & A Archive / Java Q & A Main Page