我正在尝试创建以下方法来阅读" studentmarks.txt"文件。但是,我无法将学生的标记作为诸如65 60 52的int读取并存储到数组中。它不断输出错误" java.util.InputMismatchException null"。如何在不改变" studentmarks.txt"的格式的情况下解决这个问题。文件?谢谢!
public void readMarksData(String fileName) throws FileNotFoundException
{
File dataFile = new File(fileName);
Scanner scanner = new Scanner(dataFile);
String nameOfCohort = scanner.nextLine(); //1
System.out.println(nameOfCohort);
int noOfMarks = scanner.nextInt(); //2
System.out.println(noOfMarks);
scanner.nextLine();
while( scanner.hasNext() )
{
scanner.useDelimiter("[,\n]");
String name = scanner.next(); //3
System.out.println(name);
// int marks[] = new int[3];
// for(int i = 0 ; i <= 3 ; i++)
// {
// marks[i] = scanner.nextInt();
// }
int marks[] = new int[100];
int markOne = scanner.nextInt(); //4 java.util.InputMismatchException null
marks = new int[markOne];
System.out.println(markOne);
scanner.nextLine();
int markTwo = scanner.nextInt(); //5
marks = new int[markTwo];
scanner.nextLine();
int markThree = scanner.nextInt(); //6
marks = new int[markThree];
scanner.nextLine();
//
//System.out.println(markOne + " " + markTwo + " " + markThree);
}
scanner.close();
}
studentmarks.txt:
CS1 Group 2
3
Andreas Antoniades
65 85 77
Charlotte Brocklebank
87 93 81
suzanne dawson
0 55 42
StudentRecord课程:
public class StudentRecord
{
private String name;
private String noOfMarks;
private int[] marks;
public StudentRecord(String name)
{
marks = new int[24];
this.name = name;
}
答案 0 :(得分:1)
int result = Integer.parseInt(number);
您可以使用parseInt(String val)
方法将字符串值65解析为整数值,并将数组中的存储解析为