Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

exception handling resulting in infite loop(java)

Hello I am a Java beginner.I wanted to make a method that takes an integer between 1 and 9. I am using the exception handler so that it can deal with wrong or mismatched input but it seems that it is only executing the statement "choice = input.nextInt()" once and my loop goes to infinite because of it.

Code is below:

import java.util.*;

public class Player{
    private int[] over;
    private int choice;
    private int coordinates[];
    private static Scanner input = new Scanner(System.in);


    public Player(){
        over = new int[5];
        for(int i = 0; i < 5; i++){
            over[i] = 1;
        }
        coordinates = new int[2];
        coordinates[0] = coordinates[1] = -1;
    }


    public void getChoice(){
            int choice = -1;
            boolean inputIsOk;
            do{
                System.out.print("Enter Your Choice: ");
                inputIsOk = true;
                try{
                    choice = input.nextInt();
                }
                catch(InputMismatchException e){
                    System.out.println("Invalid choice");
                    inputIsOk = false;
                }
                if(choice < 1 || choice > 9){
                    System.out.println("Enter Choice In Range(1-9)");
                    inputIsOk = false;
                }
            }while(!inputIsOk);
            System.out.println("You Entered "+choice);
    }
} 

And here is the testclass:

public class TestPlayer{
    public static void main(String args[]){
        Player p1 = new Player();
        p1.getChoice();
    }
}

Here is the ouput: First case When only integral choice is entered

harsh@harsh-Inspiron-3558:~/java/oxgame$ javac TestPlayer.java 
harsh@harsh-Inspiron-3558:~/java/oxgame$ java TestPlayer 
Enter Your Choice: 10
Enter Choice In Range(1-9)
Enter Your Choice: -1
Enter Choice In Range(1-9)
Enter Your Choice: 55
Enter Choice In Range(1-9)
Enter Your Choice: 5
You Entered 5

And Second when I enter wrong input:

Enter Your Choice: 10
Enter Choice In Range(1-9)
Enter Your Choice: 55
Enter Choice In Range(1-9)
Enter Your Choice:g
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
Enter Your Choice: Invalid choice
Enter Choice In Range(1-9)
and it goes on....

Please help me,Thanks.

like image 412
Harsh Dave Avatar asked Jun 24 '26 23:06

Harsh Dave


1 Answers

if you change catch clause as below:

} catch (InputMismatchException e) {
    input.next();
    System.out.println("Invalid choice");
    inputIsOk = false;
}

this will work, input.next(); i don't know exactly why, the old code -when you enter g- was just executing this choice = input.nextInt(); as if it still hold the same value, it did not wait for user input, calling next() fixed this.

like image 192
Yazan Avatar answered Jun 27 '26 12:06

Yazan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!