【问题标题】:Continue reading file after catching exception捕获异常后继续读取文件
【发布时间】:2016-04-28 12:22:10
【问题描述】:

我有一个由数字组成的 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");
    }
}

【问题讨论】:

  • 您的 NumberFormatException 在 while 循环之外被捕获,我建议您验证 try-、while- 和 catch-语句的括号。
  • 你的 try catch 无效,你有 2 个 try 一个没有关闭。如果您不想对代码进行重大更改,您还应该在 while 循环本身中选择 numberformatexception。
  • 顺便说一句 while((line != null)) 当你在 while 中处理异常时,它永远不会退出,你必须这样做。
  • 如果你的缩进不是那么随意,那么很容易看出哪些块在哪些块中
  • 谢谢大家,你们帮了大忙。非常感谢

标签: java file io bufferedreader


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    这很简单:你有一个从文件读取的循环。但是您的 catch 不在该循环之外。所以,当有东西抛出,而你在循环之外“抓住”时,那个循环就“结束”了;没有办法回来。

    因此,直接的答案是:将 NumberFormatException ... 的 try/catch 移动到它实际可能发生的一个位置。:

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

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

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

    【讨论】:

    • 非常感谢我现在理解的反馈,我确实看到了我的错误以及哪里出错了
    【解决方案3】:

    将 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);
        }
    }
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        使用下面的代码:

         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();
                    }
        
                }
        

        【讨论】:

          猜你喜欢
          • 2020-07-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-07
          • 1970-01-01
          • 2017-04-04
          • 2012-09-15
          • 2012-02-08
          • 1970-01-01
          相关资源
          最近更新 更多