Jorge Padilla
Jorge Padilla

Reputation: 67

How to avoid infinite loop when using try/catch?

I have this code

public void askUserForStrategy(){

    try{

        System.out.println("What strategy do you want to use?\n");
        System.out.println("1 = Math.random\t     2 = System time\t "
        + "3 = Sum of Math.random and System time");

        int strategy = sc.nextInt();

        selectStrategy(strategy);

    }

    catch(InputMismatchException Exception){

        System.out.println("That is not an integer. Try again.\n");
        askUserForStrategy();

    }

}

What I want it to do is basically ask the user to type in an integer, and in case the user the users types a String for example, catch that exception and start the method again (ask the user to type in an integer value). But the method loops when the user types in a String.

Upvotes: 0

Views: 2544

Answers (4)

Uma
Uma

Reputation: 1

This may be you are looking for..

public void askUserForStrategy() {
        while (true) {
            try {
                Scanner sc = new Scanner(System.in);
                System.out.println("What strategy do you want to use?\n");
                System.out.println("1 = Math.random\t     2 = System time\t " + "3 = Sum of Math.random and System time");

                int strategy = sc.nextInt();
                System.out.println("Selected strategy : " +strategy);
                break;
            } catch (Exception e) {
                System.out.println("That is not an integer. Try again.\n");
                continue;
            }
        }
        // selectStrategy(strategy);
    }

If user selects String, then again it will ask for options..

If user selects Integer, then it will take user selected strategy option and continue the program flow.. (Means come out of the while loop with the help of break and then calling selectStrategy method)

Thanks

Upvotes: 0

Haagenti
Haagenti

Reputation: 7397

Maybe you're looking for something like this

public void askUserForStrategy(){
Boolean loopFlag = true;
while(loopFlag) {
try{

    System.out.println("What strategy do you want to use?\n");
    System.out.println("1 = Math.random\t     2 = System time\t "
    + "3 = Sum of Math.random and System time");

    int strategy = sc.nextInt();
    Integer.parseInt(strategy);
    loopFlag  = false;
    selectStrategy(strategy);
}

catch(Exception e){

    //Parse has failed due to wrong input value, But loop will continue

}}}

Upvotes: 1

NYG
NYG

Reputation: 1817

When nextInt() throws an exception, the Scanner object tries to use the same string on the next call.

Try allocating a new Scanner Object within the try. Or try to call nextLine() within the catch, so you will discard the illegal line.

Please note that this method is not good, because after too many illegal inputs (very many, but the ideal is to have infinite tries) a stack overflow will occur.

I suggest you to use a do-while and return at the end of the try body.

Upvotes: 4

ufok
ufok

Reputation: 193

Try this:

public void askUserForStrategy() {

 for(int i=0; i<1; ++i) {
  try{

    System.out.println("What strategy do you want to use?\n");
    System.out.println("1 = Math.random\t     2 = System time\t "
    + "3 = Sum of Math.random and System time");

    int strategy = sc.nextInt();

    selectStrategy(strategy);
    break;  //break loop when there is no error
  }

  catch(InputMismatchException Exception){

    System.out.println("That is not an integer. Try again.\n");
    //askUserForStrategy();
    continue; //for clarity

  }
 }
}

Upvotes: 1

Related Questions