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
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
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
Thank you very much! Your answers and code examples are most helpful.
Tina