捕获异常后继续读取文件

Continue reading file after catching exception

我有一个 data.txt 文件,其中包含数字
12 45 345 500 45.67684 33

一旦执行了 readData 方法,它应该只打印整数值和打印对任何其他类型无效的元素,然后像这样继续

readData("data.txt") 将 打印出以下内容: 12 45 345 500 元素无效 33

我的问题是,一旦打印了元素无效语句,我的代码就不会继续打印 33,它只会停在 12 45 345 500 元素无效

import java.io.*;
import java.util.*;

public class Ex7 {

    public static void readData(String nameFile){

       try{
        BufferedReader br = new BufferedReader(new FileReader(nameFile));
        String line = br.readLine();
        int i = 0; 

         while((line != null)){
            try{
            String[] lines = line.split(" ");
            int[] result = new int[lines.length];
            result[i] = Integer.parseInt(lines[i]);

            if(result[i] ==(int)result[i]){
               System.out.print(result[i] + " ");
            }
            i++;
        }

       }
        }catch(NumberFormatException e){
            System.out.println("element not valid " );
         }catch(IOException e){
          System.out.println("IO error");
          System.exit(0);
        }
    }

    public static void main (String[] args){
        readData("data.txt");
    }
}

我建议不要使用异常,而是检查它是否是一个数字。异常很慢,应该只用作后备。检查此 link 以获取有关数字字符串的更多信息或搜索 google 以获取其他方式。 Numeric

将捕捉 NumberFormatException 的 catch 块放入循环中,但也要考虑@Христо Стайков 给出的答案。

您提出问题的原因是您不了解所发生的控制流程。

这很简单:你有一个从文件中读取的循环。但是您的 catch 在该循环之外。所以,当有东西抛出,而你 "catch" 在循环之外时,那个循环就是 "over";回不去了。

所以,直接的答案是:将 NumberFormatException 的 try/catch 移动到 一个 它实际可能发生的地方。:

try {
  result[i] = Integer.parseInt(lines[i]);
 } catch (NumberFormatException ...

但是当然...这会直接导致您的代码出现下一个问题:您正在使用数组来存储您的 "valid" 输入...数组具有固定大小。您如何提前知道有多少成员有效?你不知道。因此:您必须从使用数组改为使用动态列表,例如 ArrayList。现在您只需添加 "valid" 行即可。但是,好吧,这并不重要。因为您的方法是 而不是 返回收集的值。但是如果不使用数据;一开始就没有收藏的意义

除了您必须做出的代码更改之外的答案:回到书本上; 了解 您正在使用的 constructs/concepts 是如何工作的。盲目地键入 try/catch 是没有意义的,只是因为它们出现在某个地方,或者因为你的 IDE 告诉你需要以某种方式对异常做一些事情。意思是:当你写下代码时......确保你真的理解这段代码在做什么。你知道,就像其他声明 if (result[i] == (int) result[i]) 一样......那将永远是正确的;而且根本没有任何意义。

将 NumberFormatException 放入 While 中应该有效。

public static void readData(String nameFile){

   try{
        BufferedReader br = new BufferedReader(new FileReader(nameFile));
        String line = br.readLine();
        int i = 0; 

        while((line != null)){
            try{
                String[] lines = line.split(" ");
                int[] result = new int[lines.length];
                result[i] = Integer.parseInt(lines[i]);

                if(result[i] ==(int)result[i]){
                   System.out.print(result[i] + " ");
                }
                i++;
            }catch(NumberFormatException e){
                System.out.println("element not valid " );
            }
        }
    }catch(IOException e){
      System.out.println("IO error");
      System.exit(0);
    }
}

使用以下代码:

 public static void readData(String nameFile) {

            try {
                BufferedReader br = new BufferedReader(new FileReader(nameFile));
                String line = br.readLine();
                String[] lines = line.split(" ");
                int[] result = new int[lines.length];
                for(int i=0;i<lines.length;i++){
                    try{
                    result[i] = Integer.parseInt(lines[i]);
                    if(result[i] ==(int)result[i]){
                           System.out.print(result[i] + " ");
                    }
                    }catch(NumberFormatException nfe){
                        System.out.println("Number Invalid");
                    }
                }
            }catch(Exception ex){
                ex.printStackTrace();
            }

        }