所以我一直盯着代码(不是很有效率)大约一个小时。我有一个看起来像这样的 .txt 文件...
3
CSCE 155A - Fall 2011
4
Anthony Hopkins 80 90 95 87 80 78 25 17 20 22 21 24 19 22 21 23 24 21 20 25 20 55 56 110 30 20 25 8
John Smith 99 95 82 72 64 52 15 14 11 21 25 12 19 20 21 23 21 12 12 10 15 50 50 60 25 15 20 9
Pan Mei 85 92 72 45 82 78 22 13 16 22 24 10 18 12 21 24 25 10 11 14 20 58 51 95 28 14 28 7
Rafael Vega 99 45 87 52 87 99 25 25 21 21 14 19 19 25 25 20 20 18 20 24 20 60 60 60 25 16 23 8
CSCE 155A - Spring 2012
1
Paul Kubi 80 90 5 87 80 0 25 0 20 22 21 24 19 22 21 0 24 21 20 25 20 0 0 0 30 20 25 8
CSCE 155A - Fall 2012
3
Tianna Delp 99 99 99 99 99 99 24 15 16 21 25 15 19 20 21 22 21 21 23 15 15 60 50 60 20 17 20 9
Taylor Delp 95 92 80 90 82 78 25 25 25 25 24 10 25 25 25 25 25 25 25 25 25 58 51 95 28 14 28 7
Rachel Valenz 99 45 87 52 87 99 25 25 21 21 14 19 19 25 25 20 20 18 20 24 20 60 60 60 25 16 23 8
我需要使用数组从这个 .txt 文件中读取字符串和整数。这些数字是需要阅读的各种家庭作业和考试成绩,它们混合在一起,这意味着并非所有的家庭作业整数都在一起。每学期学生名单开始前的数字和顶部的数字分别代表该班级的学生人数和学期数。我想知道如何使用数组来读取此文件并将其应用于代码中的整数...也将使用 for 循环,但我还没有那么远...这是我到目前为止所得到的
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Student {
int i = 1;
String[] string = new String[i];
int[] integer = new int[i];
//initialize data members
private Scanner scanner;
private String firstName;
private String lastName;
private int assignmentScore;
private int labScore;
private int quizScore;
private int homeworkScore;
private int midterm1Score;
private int midterm2Score;
private int finalExamScore;
private int zyanteScore;
private int patScore;
private int testScore;
private String letterGrade;
public void openFile(){ //method to open the grades.txt file
try { //start try statement
scanner = new Scanner(new File("gradesA5.txt")); //initialize scanner to scan from the grades.txt file
} //end try statement
catch (FileNotFoundException e) { //start catch statement
System.out.println("Error opening file. Please make sure that you have a grades.txt file in the same folder as GradeCalculator.class"); //print statement telling user that the grades.txt file is not in the right place
System.exit(0); //system exit
} //end catch statement
} //end openFile method
public void setfirstName(){
string [i] = scanner.next();
i++;
System.out.println(string [0]);
}
public void setlastName(){
}
public void setAssignmentScore(){
}
public void setLabScore(){
}
public void setQuizScore(){
}
public void setHomeworkscore(){
}
public void setMidterm1Score(){
}
public void setMidterm2Score(){
}
public void setFinalExamscore(){
}
public void setZyanteScore(){
}
public void setPATScore(){
}
public void setTestScore(){
}
public void setLetterGrade(){
}
}
我放入打印语句以测试它是否正在读取并正确分配
以及调用方法的单独类...
public class CourseStatistics {
public static void main(String[] args) {
Student myStudent = new Student();
myStudent.openFile();
myStudent.setfirstName();
}
}
....以及我得到的错误
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
at Student.setfirstName(Student.java:46)
at CourseStatistics.main(CourseStatistics.java:10)
提前感谢您的帮助!