Continue a while loop after exception

前端 未结 6 495
春和景丽
春和景丽 2021-01-16 12:39

i have this piece of code. I wanted to return to the beginning of loop and ask for user input again. However, it always loops without stopping to ask for input. What is wron

相关标签:
6条回答
  • 2021-01-16 12:59

    This should throw and catch the exception and the continue command should send you back to your while loop. you need either a continue or a flag to tell your while when it stops being true.

    while(true)
    {
    try 
    {
       int choice = input.nextInt();
       throw new InputMismatchException();
    
    } 
    catch (InputMismatchException e)
    {
    continue;
    }
    }
    
    0 讨论(0)
  • 2021-01-16 13:01

    From http://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextInt%28int%29 :

    "If the translation is successful, the scanner advances past the input that matched."

    Ah, but what if the translation is not successful? In that case, the scanner does not advance past any input. The bad input data remains as the next thing to be scanned, so the next iteration of the loop will fail just like the previous one--the loop will keep trying to read the same bad input over and over.

    To prevent an infinite loop, you have to advance past the bad data so that you can get to something the scanner can read as an integer. The code snippet below does this by calling input.next():

        Scanner input = new Scanner(System.in);
        while(true){
            try {
                int choice = input.nextInt();
                System.out.println("Input was " + choice);
            } catch (InputMismatchException e){
                String bad_input = input.next();
                System.out.println("Bad input: " + bad_input);
                continue;
            }
        }
    
    0 讨论(0)
  • 2021-01-16 13:06

    You haven't posted anything asking for input,

    Scanner input = new Scanner(System.in);
    int choice;
    while (true) {
      System.out.println("Please enter an int: ");
      if (input.hasNextInt()) {                // <-- Check if there is an int.
        choice = input.nextInt();
        break;
      } else {
        if (!input.hasNext()) {                // <-- Check if there is input.
          System.err.println("No more input");
          System.exit(1);
        }
        // What ever is in the buffer isn't an int, print the error.
        System.out.printf("%s is not an int%n", input.next());
      }
    }
    // Display the choice.
    System.out.printf("choice = %d%n", choice);
    
    0 讨论(0)
  • 2021-01-16 13:07

    This works fine:

    import java.util.InputMismatchException;
    import java.util.Scanner;
    
    public class Test {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int choice;
    
            while(true){
                try {
                    choice = input.nextInt();
                    System.out.println("Your choice: " + choice);
                } catch (InputMismatchException e){
                    e.printStackTrace();
                }
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-16 13:15

    Put a line separator in your catch block.

    Scanner input = new Scanner(System.in);
    while(true)
        {
            try 
            {
                int choice = input.nextInt(); 
            } catch (InputMismatchException e)
            {
                input.next(); // Line separator
            }
    
        }
    
    0 讨论(0)
  • 2021-01-16 13:20

    Try doing do while loop.

           do
            {
                try
                {
    
                   //get user input
                    done = true; 
                } 
    
                catch (InputMismatchException e)
                {
                    System.out.println("The number entered needs to be a int");
                }
    
    
            } while (!done);
    
    0 讨论(0)
提交回复
热议问题