Question

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

  1. Enter number 1 and press enter
  2. Enter number 2 and press enter
  3. Enter nothing and press enter
  4. The program prints the multiplied numbers

But this is how the user should use the program at the moment:

  1. Enter number 1 and press enter
  2. Enter number 2 and press enter
  3. 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!

Was it helpful?

Solution 2

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);
}

OTHER TIPS

Since you're doing a school assignment, I'll make another suggestion: eliminate the confusing assignment-within-a-condition. I know you've seen this somewhere, and will continue to see it in a lot of places, and will even run into people who advocate it passionately, but I think it tends to confuse things. How about:

for (int i=0; i<2; i++)
{
  String number = reader.readLine();
  if (i == 0) { height = Integer.parseInt(number); }
         else { width = Integer.parseInt(number); }
}

Do that count < 2 check before the readLine(), otherwise it will try to read a number before it checks the count.

I.e those checks are evaluated left to right

use java.util.Scanner class for user input

Scanner scanner = new Scanner(System.in);
int width = scanner.nextInt();
int height = scanner.nextInt();
scanner.close();
System.out.println( width * height ); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top