Exception handling, catch causes while loop to stop

后端 未结 3 1681
醉话见心
醉话见心 2020-12-21 06:52

I have a file that I need to read, print out the integers, catch exception and continue with the next integer to display, and so on until there are no more integers.

相关标签:
3条回答
  • 2020-12-21 07:18

    The try/catch block needs to be inside the loop.

    When an exception is thrown, control breaks out as far as it can until it encounters a catch block, which in your case, is outside of your loop.

    public static void readNumbers()
    {
    
        File inputFile = new File ("C:/users/AC/Desktop/input.txt");
        try {
            Scanner reader = new Scanner(inputFile);
            while(reader.hasNext())
            {
                try
                {
                    int num = reader.nextInt();
                    System.out.println("Number read: " +num);
                }
                catch (InputMismatchException e)
                {
                    System.out.println("Input error ");
                }
            }
        }
        catch (FileNotFoundException e2)
        {
            System.out.println("File not found!");  
        }
    
    }
    

    I've tried putting everything into a while loop and it loops endlessly with the input exception.

    You mentioned that you tried this already. I need more details on the problem you encountered because this is the right way to do it. Off the top of my head, just a hunch, is that perhaps reader.nextInt() does not advance the reader's position in the file when the exception occurs and therefore calling nextInt again and again reads the same non-integer chunk.

    Perhaps your catch block needs to call reader.getSomethingElse? Like reader.next()?

    This is an idea and I have not tested it:

    public static void readNumbers()
    {
    
        File inputFile = new File ("C:/users/AC/Desktop/input.txt");
        try {
            Scanner reader = new Scanner(inputFile);
            while(reader.hasNext())
            {
                try
                {
                    int num = reader.nextInt();
                    System.out.println("Number read: " +num);
                }
                catch (InputMismatchException e)
                {
                    System.out.println("Input error ");
                    reader.next();   // THIS LINE IS NEW
                }
            }
        }
        catch (FileNotFoundException e2)
        {
            System.out.println("File not found!");  
        }
    
    }
    

    [Edit 9:32PM]

    I am correct about advancing the reader.

    Per the Java doc for Scanner:

    Scans the next token of the input as an int. This method will throw InputMismatchException if the next token cannot be translated into a valid int value as described below. If the translation is successful, the scanner advances past the input that matched.

    http://docs.oracle.com/javase/7/docs/api/

    0 讨论(0)
  • 2020-12-21 07:33

    Put the try catch in the loop like:

    public static void readNumbers()
    {
        File inputFile = new File ("C:/users/AC/Desktop/input.txt");
    
        try
        {  
            Scanner reader = new Scanner(inputFile);
            while(reader.hasNext())
            {
                try
                {
                    int num = reader.nextInt();
                    System.out.println("Number read: " +num);
                }
                catch (InputMismatchException e)
                {
                    System.out.println("Input error ");
                }
            }
        }
        catch (FileNotFoundException e2)
        {
            System.out.println("File not found!");
        }  
    }
    

    Edit: Note that this code causes the loop to infinitely loop on the first line that causes an InputMismatchException. Note accepted answer for the fix to this bug.

    0 讨论(0)
  • 2020-12-21 07:35

    When exception occurs, control get to the matching catch block and then subsequent line after that catch block. In your case matching catch is outside the while loop and hence while loop is stopped. Move the corresponding catch block in while loop. In your code reader.nextInt(); is the potential line which may cause the InputMismatchException.

            try {
                int num = reader.nextInt();
                System.out.println("Number read: " +num);
            } catch (InputMismatchException e) {
                System.out.println("Input error ");
            }
    
    0 讨论(0)
提交回复
热议问题