1

I'm having a little trouble with a java array and was hoping for some assistance. I am trying to make a program that will take information from 3 files - one that includes student last names, one that includes their gpa, and one that includes their student number. However, when I run the program I get the afore mentioned ArrayIndexOutOfBoundsException as:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    at Final.loadArrays(Final.java:40)
    at Final.main(Final.java:25)

I have made sure the files contain data and no additional spaces / etc. Please find my code below, and thanks for your assistance.

import java.util.Scanner;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.swing.JOptionPane;

public class studentinfo
{

public static void main(String [] args) throws FileNotFoundException

{
final int MAX_SIZE = 3;
String[] names = new String[MAX_SIZE];
double[] gpa = new double[MAX_SIZE];
int[] studentNumber = new int[MAX_SIZE];

loadArrays(names, gpa, studentNumber);

}
public static void loadArrays(String[] names, double[] gpa, int[] studentNumber) throws FileNotFoundException

{
    Scanner namesInFile = new Scanner(new FileReader ("names.txt"));
    Scanner gpaInFile = new Scanner(new FileReader ("gpa.txt"));
    Scanner studentNumberInFile = new Scanner(new FileReader ("studentNumber.txt"));

    int i = 0;
    //loop for loading names array
    while(namesInFile.hasNext())

    {
        names[i] = namesInFile.next();
        i++;

    }

    i = 0;
    //loop for loading gpa array
    while (gpaInFile.hasNext())
    {
        gpa[i] = gpaInFile.nextDouble();
        i++;

    }
    i = 0;
    //loop for loading student number array
    while (studentNumberInFile.hasNext())
    {
        studentNumber[i] = studentNumberInFile.nextInt();
        i++;

    }


    namesInFile.close();
    gpaInFile.close();
    studentNumberInFile.close();


    for(i = 0; i < names.length ; i++)
            System.out.println(names[i]);

    for(i = 0; i < gpa.length ; i++)
            System.out.println(gpa[i]);

    for(i = 0; i < studentNumber.length ; i++)
            System.out.println(studentNumber[i]);
        String message = "";
    for(i = 0; i < gpa.length ; i++)
            message += studentNumber[i]+" "+names[i]+" "+gpa[i]+"\n";

        JOptionPane.showMessageDialog(null, message);
}

}

  • Can you post the input files as well? – Sweeper Jul 29 '18 at 00:39
  • 1
    I took your code and ran it in Eclipse with no issues - with that being said I can tell you most likely have a couple of issues: 1) The file that you are pointing at has more than 3 entries 2) You have some unknown characters in your files (maybe an additional line break?) add system out's to your code and that will tell you where it is breaking :) – Bob Jul 29 '18 at 01:00
  • Thank you so much Bob, that answered my question. I was mistaken in thinking that the MAX_SIZE had to do with elements in the array, not the number of entries in the file. I appreciate it! – BiscuitWrangler Jul 29 '18 at 02:54

2 Answers2

0

You declare the max size at the top as

final int MAX_SIZE = 3;

You need to be sure that the number of contents in your documents is less than MAX_SIZE, else you will get this exception.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
zpd
  • 1
  • 1
0

3 is your file number not the number of students in your files. Do not use array since you don't know the actual number, use List instead.

import java.util.ArrayList;

Change array to array list.

ArrayList<String> names = new ArrayList<>();
ArrayList<Double> gpa = new ArrayList<>();
ArrayList<Integer> studentNumber = new ArrayList<>();

Use add and get(index).

names.add(namesInFile.next());

And

System.out.println(names.get(i));
zhh
  • 2,346
  • 1
  • 11
  • 22