我有一个需要读取的文件,打印出整数,捕获异常并继续显示下一个整数,依此类推,直到没有整数。
该文件包含:12 5 sd 67 4 cy
我希望它显示:
12 
5
输入错误
67 
4
输入错误
但是,它只给了我 12、5,然后是输入错误,然后它就停止了。我已经尝试将所有内容都放入一个while循环中,并且它会随着输入异常而无休止地循环。
public static void readNumbers()
 {
    File inputFile = new File ("C:/users/AC/Desktop/input.txt");
     try
     {
         Scanner reader = new Scanner(inputFile);
         while(reader.hasNext())
         {
                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!");
      }    
  }
 }
我错过了什么,以便循环继续读取下一个 int 等等?