我在我的計算機中有一個文本文件,我正在閱讀窗體的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());
}
}
'notepad'是工具不是一個文件,你是從文本文件中讀取。 –
我只是用提供的數據運行你的代碼,並且沒有錯誤。你確定你使用的是同一組數據嗎? (在文本文件中) – ssssteffff
BufferedReader br = new BufferedReader(new InputStreamReader(fstream,「UTF8」)); 幫我 – user2131465