2016-04-28 78 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(); 
      } 

     }