2016-04-28 146 views
0

Ive得到一个的data.txt文件之后读文件一次READDATA方法执行它应该只打印哪些是整数,打印元件的值,其包括数字
12 45 345 500 33 45.67684继续捕捉异常

的无效则任何其它类型的继续这样

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

你NumberFormatException的是擦肩而过,而循环之外,我劝你验证试戴的支架,while-和追赶的语句。 – Dominique

+0

你的尝试抓住是无效的,你有2个尝试一个不关闭。如果你不想在代码中做大的改变,你也应该在while循环本身中选择numberformatexception。 – MrSimpleMind

+0

Btw while((line!= null))当你处理异常时,你永远不会退出。 –

回答

1

你的问题的原因是你不明白发生的控制流。

这很简单:你有一个从文件读取的循环。但你的捕获是在该循环之外。所以,当一些事情发生时,你在循环之外“捕捉”,那个循环就“结束”了。没有办法回来。

所以,直接的答案是:移动的try/catch为NumberFormatException的......到它实际发生的:

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

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

而且你必须做出的代码变更之外的答案:回头看书;和学习你正在使用的构造/概念实际上是如何工作的。没有必要盲目地输入try/catch,只是因为它们出现在某处,或者因为IDE告诉你某种方式需要对异常做些什么。意思是:当你写下代码时...确保你真的很懂了解这段代码正在做什么。你知道,就像其他陈述if (result[i] == (int) result[i]) ......那将永远是真的;根本没有任何意义。

+0

非常感谢我现在了解的反馈,我确实看到了我的错误以及我出错的地方 – helpmewithjava

2

我不会使用异常提示,但一检查,如果它是一个数字。异常缓慢,应仅用作后备。查看此链接以获取有关数字字符串的更多信息,或者通过其他方式搜索Google。 Numeric

0

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

1

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

     }