0

Can you please tell me what is wrong with my code. It is supposed to ask for input until both inputs entered are integers

public static void main(String[] args) {
    int a, b;
    Scanner input = new Scanner(System.in);
    int i = 0;

    while (i == 0) {
        try {

            System.out.print("Enter first number");
            a = input.nextInt();
            System.out.println("The number you entered is " + a);
            System.out.println("Enter second number");
            b = input.nextInt();
            System.out.println("The number you entered is " + b);

            i++;
        } catch (InputMismatchException e) {
            e.printStackTrace();
        }
    }
    System.out.println("End");
}
1
  • Think about implementing a method that requests a single integer (and it should ask again if the input was invalid). Then call this method twice. This is much easier, than requesting and checking two valid individual numbers. Also the user doesn't need to re-enter the first number, if the second one was invalid. Commented Feb 21, 2015 at 23:30

2 Answers 2

2

If an invalid integer is entered that content will remain in the input buffer and be passed through to the exception block ad infinitum. These invalid characters need to be consumed using next or nextLine. hasNextInt should be used to check that an integer is actually available before calling nextInt

Sign up to request clarification or add additional context in comments.

Comments

-1

Your Looping condition is :

int i = 0;
while (i == 0) {  // Looping against i==0
    try {
        System.out.print("Enter first number");
        a = input.nextInt();
        System.out.println("The number you entered is " + a);
        System.out.println("Enter second number");
        b = input.nextInt();
        System.out.println("The number you entered is " + b);
        i++; // Increments the value of i to 1, which makes the loop to exit after first iteration
    } catch (InputMismatchException e) {
        e.printStackTrace();
    }
}

Refactor your code as :

boolean areBothIntegers = true;
        while (areBothIntegers) {
            try {
                System.out.print("Enter first number");
                a = input.nextInt();
                System.out.println("The number you entered is " + a);
                System.out.println("Enter second number");
                b = input.nextInt();
                System.out.println("The number you entered is " + b);
            } catch (InputMismatchException e) {
                e.printStackTrace();
                areBothIntegers = false;
            }
        }

Sample Output :

Enter first number : 10

The number you entered is 10

Enter second number : 21

The number you entered is 21

Enter first number : 21

The number you entered is 21

Enter second number : 34

The number you entered is 34

Enter first number : 21.5 // Not an int value, so throws the exception and exits

java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at jdbc.statements.ADS.main(ADS.java:15)

10 Comments

@Tom Yes it does..after then only i posted this.
And this code asks the user for input until he entered two valid numbers?
It is supposed to ask for input until both inputs entered are integers That is what OP tries to accomplish. Your loop asks for more input as long the user enters numbers. This is the direct opposite.
It actually asks for input as long as both inputs are valid, however I got the point .. respect!
@Armitesh Your view is right but your code does not reflect your view
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.