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
- Enter number 1 and press enter
- Enter number 2 and press enter
- Enter nothing and press enter
- The program prints the multiplied numbers
But this is how the user should use the program at the moment:
- Enter number 1 and press enter
- Enter number 2 and press enter
- The program prints the multiplied numbers
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!