Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3

0

As much as I tried to identify the error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3 and I could not find it. My code is:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Exercise31 {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        System.out.print("Enter year: ");
        String year = input.next();

        System.out.print("Enter gender: ");
        String gender = input.next();

        System.out.print("Enter name: ");
        String name = input.next();
        input.close();

        File file = new File("babynameranking" + year + ".txt");
        if (!file.exists()) {
            System.out.println("No record for " + year);
            System.exit(1);
        }

        int rank = 0;
        try {
            Scanner read = new Scanner(file);
            while (read.hasNext()) {

                String s = read.nextLine();
                String[] temp = s.split(" ");

                if (gender.equalsIgnoreCase("M") && temp[1].contains(name))
                    rank = new Integer(temp[0]);
                else if (temp[3].contains(name)) // aqui me marca el error 
                    rank = new Integer(temp[0]);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        if (rank == 0) {
            System.out.println("The name "+name+" is not ranked in year " + year);
        } else {

            System.out.println(name+" is ranked #"+rank+" in year "+ year);
        }


    }
}

"babynameranking.txt" are ten files with the same name but with different YEAR from 2001 to 2010 in which there are lists of names of women and men listed according to popularity ranking in each YEAR so I have to do that when I between the YEAR, the sex and the name show me the number of the ranking.

    
asked by J.Vieyra 07.08.2017 в 22:05
source

1 answer

2

To understand why your error happens first a little information.

When you use the split () in java you get an array, as an example:

 String s = "John Vieyra Garza Tepes";
 String[] temp = s.split(" ");

In this case, the array has 4 elements, and you can get its values through the index

  • temp[0] contains the value John
  • temp[1] contains the value Vieyra
  • temp[2] contains the value Garza
  • temp[3] contains the value Tepes

What happens if you try to get the array element with index 3 of the following string:

 String s = "John Vieyra";
 String[] temp = s.split(" ");

You can get temp[0] and temp[1] but the element that you try to get temp[3] does not exist, when trying to obtain it you will get the following error:

  

java.lang.ArrayIndexOutOfBoundsException: 3

Therefore you must ensure each line of your file has 4 or more elements so that you do not have this problem at this point:

 String s = read.nextLine();
 String[] temp = s.split(" ");

or first validate that there is no null value in the array element in this line of code:

else if (temp[3].contains(name)) // aqui me marca el error 
    
answered by 08.08.2017 в 01:38