异常处理,捕获原因while循环停止 [英] Exception handling, catch causes while loop to stop

查看:306
本文介绍了异常处理,捕获原因while循环停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要读取的文件,打印整数,捕获异常并继续显示下一个整数,依此类推,直到不再有整数。



该文件包含:12 5 sd 67 4 cy



我想要显示:



12

5

输入错误

67

4

输入错误



但是,它只给我12,5,然后输入错误,它停止。
我已经尝试将所有内容都放在一个循环中,并且随着输入异常循环而循环。

  public static void readNumbers()
{

文件inputFile =新文件(C:/users/AC/Desktop/input.txt);
尝试
{
扫描仪阅读器=新的扫描仪(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等等?

解决方案

try / catch块需要在循环。



当抛出异常时,控件会尽可能地发生,直到遇到一个catch块(在你的情况下,它不在你的循环之外) / p>

  public static void readNumbers()
{

文件inputFile =新文件:/users/AC/Desktop/input.txt);
尝试{
扫描仪阅读器=新的扫描仪(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!);
}

}




我已经尝试把所有的东西放在一个循环中,并且循环不断地
与输入例外。


你提到你已经试过了我需要更多关于你遇到的问题的细节,因为这是正确的方法。离开我的头顶,只是一个希望,也许reader.nextInt()不会提高读者在异常发生时在文件中的位置,因此一次又一次地调用nextInt读取相同的非整数块。



也许你的catch块需要调用reader.getSomethingElse?像reader.next()?



这是一个想法,我还没有测试:

  public static void readNumbers()
{

文件inputFile =新文件(C:/users/AC/Desktop/input.txt);
尝试{
扫描仪阅读器=新的扫描仪(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(); //这条线是新
}
}
}
catch(FileNotFoundException e2)
{
System.out.println(File not found! );
}

}



我对推进读者是正确的。



根据扫描器的Java文档:


将输入的下一个标记作为int进行扫描。如果下一个令牌不能被翻译成
有效的int值,则此方法将抛出
InputMismatchException,如下所述。 如果翻译成功,
,扫描器将超过匹配的输入。


< a href =http://docs.oracle.com/javase/7/docs/api/ =nofollow> http://docs.oracle.com/javase/7/docs/api/


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.

The file contains: 12 5 sd 67 4 cy

I want it to display:

12
5
Input error
67
4
Input error

However, it only gives me 12, 5, followed by input error, and it stops. I've tried putting everything into a while loop and it loops endlessly with the input exception.

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!");
      }    
  }

 }

What am I missing so that the loop continues reading the next int and so on?

解决方案

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/

这篇关于异常处理,捕获原因while循环停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆