2013-07-11 43 views
1

我在我的計算機中有一個文本文件,我正在閱讀窗體的Java程序,我想構建一些標準。這裏是我的記事本文件:數組超出約束的異常

#Students 
    #studentId studentkey yearLevel studentName token 
    358314   432731243 12   Adrian  Afg56  
    358297   432730131 12   Armstrong YUY89  
    358341   432737489 12   Atkins  JK671 

     #Teachers 
     #teacherId teacherkey yearLevel teacherName token 
     358314   432731243 12   Adrian  N7ACD  
     358297   432730131 12   Armstrong EY2C   
     358341   432737489 12   Atkins  F4NGH 

同時也可與下面的代碼我得到陣列外邊界異常的記事本閱讀本。在調試時,我得到了strLine.length()的「#Students」值。 任何人都可以幫助解決這個問題嗎?

private static Integer STUDENT_ID_COLUMN = 0; 
private static Integer STUDENT_KEY_COLUMN = 1; 
private static Integer YEAR_LEVEL_COLUMN = 2; 
private static Integer STUDENT_NAME_COLUMN = 3; 
private static Integer TOKEN_COLUMN = 4; 

public static void main(String[] args) { 
    ArrayList<String> studentTokens = new ArrayList<String>(); 

    try { 
     // Open the file that is the first 
     // command line parameter 
     FileInputStream fstream = new FileInputStream("test.txt"); 
     BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 
     String strLine; 
     // Read File Line By Line 
     while ((strLine = br.readLine()) != null) { 
      strLine = strLine.trim(); 

      if ((strLine.length()!=0) && (strLine.charAt(0)!='#')) { 
       String[] students = strLine.split("\\s+"); 
       studentTokens.add(students[TOKEN_COLUMN]); 
      } 


     } 

     for (String s : studentTokens) { 
      System.out.println(s); 
     } 

     // Close the input stream 
     in.close(); 
    } catch (Exception e) {// Catch exception if any 
     System.err.println("Error: " + e.getMessage()); 
    } 
} 
+0

'notepad'是工具不是一個文件,你是從文本文件中讀取。 –

+0

我只是用提供的數據運行你的代碼,並且沒有錯誤。你確定你使用的是同一組數據嗎? (在文本文件中) – ssssteffff

+0

BufferedReader br = new BufferedReader(new InputStreamReader(fstream,「UTF8」)); 幫我 – user2131465

回答

1

考慮了charakter集,也許該文件被認爲是在Unicode中,但你要求ASCII?你可以在這裏改變:

BufferedReader br = new BufferedReader(new InputStreamReader(in, charakterset)); 

這可能幫助:Java InputStream encoding/charset

1

看來你所面臨的一些問題編碼。以相同的格式保存並讀取文件。最好使用UTF-8。使用構造函數new FileInputStream(<fileDir>, "UTF8")進行閱讀。
How to save a file in unicode

+0

文件格式是UTF8。你能發送文本文件在[email protected]嗎? – user2131465

+0

您是否嘗試實施更改?如果你的文件格式是「UTF-8」,它應該可以工作。不幸的是,我目前無法訪問網絡郵件。 –

+0

BufferedReader br = new BufferedReader(new InputStreamReader(fstream,「UTF8」));幫助過我 – user2131465

1

它有可能是你的文件的編碼是不一樣的,你正在閱讀。

要麼找出您的文件的編碼或將其轉換爲UTF8,然後在您的代碼中使用下面的編碼讀取它。

你也應該改變strLine.charAt(0)!='#'!strLine.contains("#")除非是guaranteeed是第一個字符,並可能發生在其他領域的一個

而且它是好主意,調用任何異常的printStackTrace()你趕上

public static void main(String[] args) { 
    ArrayList<String> studentTokens = new ArrayList<String>(); 

    try { 
     // Open the file that is the first 
     // command line parameter 
     FileInputStream fstream = new FileInputStream(new File("C:\\Fieldglass\\workspace-Tools\\Tools\\src\\tools\\sanket.txt")); 

    // ------ See below, added in encoding, you can change this as needed if not using utf8 
     BufferedReader br = new BufferedReader(new InputStreamReader(fstream, "UTF8")); 

     String strLine; 
     // Read File Line By Line 
     while ((strLine = br.readLine()) != null) { 
      strLine = strLine.trim(); 

      if ((strLine.length()!=0) && (!strLine.contains("#"))) { 
       String[] students = strLine.split("\\s+"); 
       studentTokens.add(students[TOKEN_COLUMN]); 
      } 
     } 

     for (String s : studentTokens) { 
      System.out.println(s); 
     } 

     // Close the input stream 
     fstream.close(); 
     br.close(); // dont forget to close your buffered reader also 
    } catch (Exception e) {// Catch exception if any 
     e.printStackTrace(); 
     System.err.println("Error: " + e.getMessage()); 
    } 
} 

你可以看看這裏的Java supported encodings(如1.5)

1

您提供了不準確的信息。

從一個記事本閱讀下面的代碼,我得到數組越界的異常。

我看不出如果代碼和輸入如你所說的那樣這是可能的。我可以看到,可能會引發ArrayIndexOutOfBoundsException唯一的地方是這一行:

students[TOKEN_COLUMN] 

但我的代碼和輸入的解讀是,這得那麼遠有5個領域的每一個輸入線。當拆分時,這會給你一個包含5個元素的數組,並且students[TOKEN_COLUMN]將工作。

IMO,程序或輸入不是你所描述的。 (我的猜測是你有少於5個字段的輸入行。)

在調試時,我得到了strLine.length()的「#Students」值。

這是奇怪的難以置信的地步。 strLine.length()返回int。你向我們展示的是一個字符串。


實際上,我對發生了什麼事情有了一定的瞭解。如果"  #Students"strLine(而不是strLine.length() !!)的值,那麼你已經設法在文件開始時得到一些垃圾。當你的代碼檢查這一點,第一個字符不會是一個「#」和行會出現有2場,而不是5,這將導致異常...

而且我想我知道垃圾從哪裏來。它是一個UTF-8字節順序標記,由NotePad ...插入到文件的開頭,因爲您將該文件保存爲UTF-8。然後該文件是讀取使用CP1252 ...這是(我推測)您的系統的默認字符集。

課程:請勿使用記事本。使用real編輯器。

參考:https://en.wikipedia.org/wiki/Byte_order_mark#Representations_of_byte_order_marks_by_encoding