简体   繁体   中英

Reading from a file into an array of objects

I am trying to read data from a file that I need to be put into my array of objects. When I am trying to read 6 tests from a single student I am getting an error.

I get this error,

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6
at example.example.readStudentList(example.java:40)
at example.example.main(example.java:57)

how can I make it so I could read the 6 tests pers student and not get out of bounds?

public static Scanner openFile()throws IOException{
    File input;
    Scanner inFile ;
    String fileName;
    System.out.print("Enter input file name and path if necessary: ");//e://csc121//data.txt
    fileName = KB.nextLine();
    input = new File(fileName);
    if( ! input.exists()){
        System.out.println("File does not exists. Check the file and try again");
        System.exit(0);
    }

    inFile = new Scanner(input);  // Step 1 Initialize loop condition
    if (! inFile.hasNext()){
        System.out.println("Error. Data file has no data.\n");
        System.exit(0);
    }
    return inFile;
}
public static int readStudentList(Student[] stu)throws IOException{
    int     i = 0;
    Scanner inFile;
    String name;
    String id;
    float quiz;
    float[] tests = new float[6];
    inFile = openFile();
    
    while(inFile.hasNext()){
        name = inFile.nextLine();
        id = inFile.next();
        for(int j = 0; i < 6; j++){
        tests[j] = inFile.nextFloat();
        }
        quiz = inFile.nextFloat();
        inFile.nextLine();
        stu[i] = new Student(name, id,tests,quiz);
        
        i++;
    }
    return i;
 }   


public static void main(String[] args)throws IOException {
    

    Student[] arr = new Student[50];
    int size;
    size = readStudentList(arr);
    System.out.println(size);
    System.out.println(arr[0].name);
    System.out.println(arr[0].id);
   }  
 }


class Student {

public String id;
public String name;
public float[] tests = new float[6];
public float quiz;


Student(String name, String id, float[] tests, float quiz)
{
    this.id = id;
    this.name = name;
    this.tests = tests;
    this.quiz = quiz;


  }
}

Take a close look at your for loop:

for(int j = 0; i < 6; j++){

Notice you are incrementing j , but checking the value of i

You have used i in your if statement instead of j . Try the following:

public static int readStudentList(Student[] stu)throws IOException {
    int i = 0;
    Scanner inFile;
    String name;
    String id;
    float quiz;
    float[] tests = new float[6];
    inFile = openFile();
    
    while(inFile.hasNext()){
        name = inFile.nextLine();
        id = inFile.next();
        
        for(int j = 0; j < 6; j++){
          tests[j] = inFile.nextFloat();
        }
        
        quiz = inFile.nextFloat();
        inFile.nextLine();
        stu[i] = new Student(name, id,tests,quiz);
        
        i++;
    }
    
    return i;
 } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM