我有以下代碼讀取一行學生,程序應該在每個空白處拆分,然後轉到文本的下一部分,但是我得到了arrayindexoutofBound異常。 文本文件有幾行是這樣的:Java數組索引超出範圍
130002 Bob B2123 35 34 B2132 34 54 B2143 23 34
public static void main(String[] args) throws FileNotFoundException {
File f = new File("C:\\Users\\Softey\\Documents\\scores.txt");
Scanner sc = new Scanner(f);
List<MarkProcessing> people = new ArrayList<MarkProcessing>();
while(sc.hasNextLine()){
String line = sc.nextLine();
String[] details = line.split("\\s+");
String regNumber = details[0];
String name = details[1];
String modOne= details[2];
int courseM = Integer.parseInt(details[3]);
int examM = Integer.parseInt(details[4]);
String modTwo = details[5];
int courseM2 = Integer.parseInt(details[6]);
int examM2 = Integer.parseInt(details[7]);
String modThree = details[8];
int courseM3 = Integer.parseInt(details[9]);
int examM3= Integer.parseInt(details[10]);
MarkProcessing p = new MarkProcessing(regNumber, name, modOne,courseM, examM, modTwo,courseM2,examM2, modThree, courseM3, examM3);
people.add(p);
}
}
}
當它關係到細節[1]我得到的指數錯誤。
哪一行導致異常? – Scorpion
讓它檢查拆分數組的大小,如果它小於11,則打印混亂的線條。 – CBredlow
也許會添加文件和錯誤消息? –