0

所以我一直盯着代码(不是很有效率)大约一个小时。我有一个看起来像这样的 .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)

提前感谢您的帮助!

4

5 回答 5

0

当您i在方法中递增时setFirstName,它会超出范围,因为您声明的变量的大小为 1。尝试增加数组的大小并从索引 0 开始存储

int i = 1;

String[] string = new String[i];
int[] integer = new int[i];
于 2013-10-31T05:22:54.950 回答
0

您的代码背后的基本思想应该是:

main:
  read the number of semesters
  create an array for n Semesters
  n times:
    read a Semester and store it in the array

reading a semester:
  read the number of students
  create an array for n Students
  n times:
    read a Student and store it in the array

reading a student:
  (left as an excercise)

该方案建议您创建一个Semester包含学生的班级和一个Student包含成绩的班级。另一个名为的类GradeProcessor应该有方法readSemesterreadStudent.

于 2013-10-31T05:29:09.110 回答
0

您声明array[String]为大小11使用以下内容:

int i = 1;

String[] string = new String[i];

当你去参考[i]时,这是1和大小是1,你必须记住,第一个索引是用 表示的0。您将不得不参考0. 我也不确定你的意思是将数组初始化为 1。

public void setfirstName(){
    string [i] = scanner.next();
    i++;
    System.out.println(string [0]);
}
于 2013-10-31T05:29:52.843 回答
0

因为您在这里启动了一个大小为 1 的数组:

int i = 1;

String[] string = new String[i];

然后尝试在此处分配您的数组@索引 1,这超出了您的数组范围:

string [i] = scanner.next();

请尝试:

string [i-1] = scanner.next();

另外,我发现当您尝试将另一个字符串添加到大小为 1 的数组时,这种情况会再次发生。您是否打算向您的 Student 类添加一个动态增长的数组?

于 2013-10-31T05:23:24.827 回答
0

数组是排他的。这意味着如果您将数组设置为:int[] arr = new int[5],则索引“5”将不可用。在这种情况下,大小为 5,因此索引为0-1-2-3-4. 如果您要访问只有一个元素的数组,则必须使用arr[0].

于 2013-10-31T05:25:04.217 回答