For a school assignment I have to make a program that reads two numbers from the terminal and then processes those numbers. The program has to automatically process those two values once they have been entered. The code I have so far is down below, but you have to press Enter before the program multiplies the numbers, the user shouldn't have to press enter three times, but only two times.
public static void man(String[] args) throws NumberFormatException, IOException{
BufferedReader reader = new BufferedReader( new InputStreamReader(System.in));
int count = 0;
int width = 0;
int height= 0;
String number;
while( (number = reader.readLine())!=null && count < 2 ) {
while( count < 2 ){
if( count == 0) {
width = Integer.parseInt( number);
count++;
break;
}
else if (count == 1) {
height = Integer.parseInt( number);
count++;
break;
}
}
}
System.out.println( width * height );
}
This is how the user has to use the program at the moment
But this is how the user should use the program at the moment:
Of course my program has to do something different for the assignment but I have changed it a little bit to make it easier to explain here.
Thank you for you help in advance!
Try this modifications:
public static void main(String[] args) throws NumberFormatException,
IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
int count = 0;
int width = 0;
int height = 0;
String number;
while (count < 2) { // Just 2 inputs
number = reader.readLine();
if (count == 0) {
width = Integer.parseInt(number);
count++;
} else if (count == 1) {
height = Integer.parseInt(number);
count++;
}
else // If count >= 2, exits while loop
break;
}
System.out.println(width * height);
}