Venkada Narasiman
Venkada Narasiman

Reputation: 3

Trying to use Try catch in a while loop until the user answers correctly without entering invalid data(Java)

I am trying to create a program which asks the user to guess a number which is randomly generated and the loop exits when the input is correct. I am also trying to stop user from entering an invalid data and want the loop to repeat until user enters a valid data. The problem is when is type in an alphabet as an input, the program repeats. thank you for helping in advance. I am using eclipse kepler

Output:
Try guessing the number: k You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number: You have entered invalid data. Please try again Try guessing the number:

while(true){
         try{
            System.out.println("Try guessing the number: ");
            guess=input.nextInt();
            if(guess==sum){
                System.out.println("You have guessed it correctly");
                break;
            }
        }catch(InputMismatchException e){
            System.out.println("You have entered invalid data. Please try again");
        }
       }

Upvotes: 0

Views: 835

Answers (2)

ravensgo
ravensgo

Reputation: 522

This worked for me... It fixes the while(true) issue and uses a simpler way to check for incorrect data that is easier to debug! Enjoy and good luck with your game!

        String guessString="";
        int guess = 0;
        Scanner input = new Scanner(System.in);
        int sum = 12;
        //just used 12 as a placeholder, you'll have to connect your 
        //random number generator, as well as changing the default of 
        //guess=0 if 0 is in your range for the random number

        while(sum!=guess){

            System.out.println("Try guessing the number: ");
            guessString=input.next();
            try {
                guess = Integer.parseInt(guessString);
            } catch(Exception e) {
                System.out.println("Invalid Data.");
                guess=0;
            }

        }
        System.out.println("You have guessed it correctly");

Upvotes: 0

Yunus Emre Güler
Yunus Emre Güler

Reputation: 387

You don't have to use try/cathc structure. A while loop would be enough

while(true){
 System.out.println("Try guessing the number: ");
            guess=input.nextInt();

if(guess==sum){
                System.out.println("You have guessed it correctly");
                break;
            }
if(!(guess instanceof Int)){
System.out.println("You have entered invalid data. Please try again");

}


}

Upvotes: 1

Related Questions