-4

I am trying to read from a text file that has 20 lines and supposed to store them into an array and assign them a variable, firstname lastname and grade. Because I have to output them as last name, firstname and grade, I decided to use tokens but somehow I get this error: java.lang.ArrayIndexOutOfBoundsException: 1

public static void main(String[] args) throws IOException {
    int numberOfLines = 20; 
    studentClass[] studentObject = new studentClass[numberOfLines];
    readStudentData(studentObject);

}    
    public static void readStudentData(studentClass[] studentObject)throws  {

    //create FileReader and BufferedReader to read and store data
    FileReader fr = new FileReader("/Volumes/PERS/Data.txt");
    BufferedReader br = new BufferedReader (fr);


    String line = null;
    int i = 0;

    //create array to store data for firstname, lastname, and score
    while ((line = br.readLine()) != null){
       String[] stuArray = line.split(" ");
       String stuFName = stuArray[0];
       String stuLName = stuArray[1];
       int score = Integer.parseInt(stuArray[2]);
       studentObject[i] = new studentClass (stuFName, stuLName, score);
       i++;
    }
    br.close();
    for(i = 0; i<studentObject.length; i++){
        System.out.print(studentObject[i].getStudentFName());
    }

}

The error that I get is specifically this line:

String stuLName = stuArray[1];

Here is the text file:

Duckey Donald 85
Goof Goofy 89
Brave Balto 93
Snow Smitn 93
Alice Wonderful 89
Samina Akthar 85
Simba Green 95
Donald Egger 90
Brown Deer 86
Johny Jackson 95
Greg Gupta 75
Samuel Happy 80
Danny Arora 80
Sleepy June 70
Amy Cheng 83
Shelly Malik 95
Chelsea Tomek 95
Angela Clodfelter 95
Allison Nields 95
Lance Norman 88
  • 1
    Show your text file. If it is failing at this line it means that for a specific line there is no more then 1 element (with space), I mean `here are three` and `justone` this second will fail because the array contains only one element and will give you the arrayindex error – Jorge Campos May 25 '17 at 23:55
  • it may be helpful if you can share the `Data.txt` file to get a idean how the data is stored there. – Rajith Pemabandu May 25 '17 at 23:55
  • @JorgeCampos here you go guys – Perseus Ancheta May 25 '17 at 23:59
  • @RajithPemabandu here you go – Perseus Ancheta May 25 '17 at 23:59
  • Are you sure that there isn't a line break at the last line? It shouldn't be failing if your file is just what you show. – Jorge Campos May 26 '17 at 00:00
  • 2
    How about just adding the correct and required error handling? If the split didn't give you at least 2 words then don't try to access the 2nd word! Not good enough? Try debugging (println or debugger) to see why the split didn't give the expected results. – John3136 May 26 '17 at 00:01
  • @JorgeCampos I am sure, it stops right at line 20 – Perseus Ancheta May 26 '17 at 00:02
  • This doesn't help your immediate issue, but `line.split("\\s+")` would work better if there might be multiple spaces separating the tokens in your line. – Kevin Anderson May 26 '17 at 00:09
  • @KevinAnderson thanks for the suggestion, unfortunately, i have tried that one also prior to this, errors in the same line. – Perseus Ancheta May 26 '17 at 00:11
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) Alternative to using a debugger: Print the value of `line` before calling `split`, and/or print the value of `stuArray` using `Arrays.toString(stuArray)` right after calling `split`, so you can see what's going on. *FYI:* That's also called **debugging**, just without using a debugger tool to help you. – Andreas May 26 '17 at 00:28

2 Answers2

0

I think at the last line of your file you have white spaces. make sure last line hast no white space like space or tab.

Touraj Ebrahimi
  • 566
  • 4
  • 14
0

First, next time you should include the import and output also in your code for us to easy to fix it, and one more thing, the Class name should be StudentClass, not studentClass, it have to me different with methods. Second, I can't test your code without your studentClass ... So I only can guess it: Consider 1: The text file have one more line (with white space) >> Impossible because String test = " "; test.split(" ")[0] == null; Consider 2: Your text file has error, to test it, I suggest you to add System.out.println(line + ".") after while ((line = br.readLine()) != null){ to test it, believe me, you will receive the last line because it's bloged;