Trouble with an ArrayIndexOutOfBoundsException in Java

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

Trouble with an ArrayIndexOutOfBoundsException in Java



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
2 hours ago





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
2 hours ago





Possible duplicate of What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
– Jacob B.
2 hours ago





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
13 mins ago




2 Answers
2



final int MAX_SIZE = 3;//Are you sure that the number of contents in your documents is less more than MAX_SIZE?



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.


3


List


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).


add


get(index)


names.add(namesInFile.next());



And


System.out.println(names.get(i));






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

Trying to Print Gridster Items to PDF without overlapping contents

Hystrix command on request collapser fallback