我在尝试从Array ( [0] => 4578 [1] => kors [2] => red [3] => wedge [4] => 4|5|6|7|8|9|10 )
解析为String
或Integer
时收到错误。
double
如果我打印int id = Integer.parseInt(stringParts[2]);
它有效,它只会在解析时抛出错误。
这是我正在使用的完整循环:
stringParts[2]
它会生成错误: public static StudentRecord[] creates(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("/Users/DNA40/Desktop/lab11input.txt"));
int lineText = lineCount("/Users/DNA40/Desktop/lab11input.txt");
String record;
String cons = ("[ ]");
StudentRecord[] student = new StudentRecord[lineText];
String[] stringParts = new String[5];
for(int i = 0; i < lineText ; i++){
student[i] = new StudentRecord();//Creates Object class
record = br.readLine(); //Stores the first line of text file
stringParts = record.split("\\s+");//Splits the line into parts
student[i].setFirstName(stringParts[0]);
student[i].setLastName(stringParts[1]);
int id = Integer.parseInt(stringParts[2]);
student[i].setID(id);
double gpa = Double.parseDouble(stringParts[3]);
student[i].setGPA(gpa);
int hours = Integer.parseInt(stringParts[4]);
student[i].setHours(hours);
}
return student;
}
public static int lineCount(String fileName) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(fileName));
int count = 0;
String currentLine;
while ((currentLine = br.readLine()) != null){
count++;
}
return count;
}
谢谢
答案 0 :(得分:0)
索引越界意味着
stringParts[2]
不存在。检查阵列的长度。分裂可能正如您所期望的那样工作