1

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!

1
  • Your main method has the name man. Correct it please. Commented Oct 28, 2013 at 18:58

4 Answers 4

1

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); } } 
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

Your method is much easier to understand, so I'm going to use your method but Aaron's method also worked. Thank you for your very fast answer!
0

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

1 Comment

Thank you! This worked very well, but Christian´s method is easier to understand so I´m going use that one. Thank you very much for your fast answer!
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.