-1

array is created and the split method is used, also created an instance of my NameInfo class with the array:

public class NameSearch {

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

    NameInfo[] nameList = new NameInfo[151671];

    Scanner keyboard = new Scanner(System.in);
    String inString = null;
    String selectedName;
    String anotherName;
    int numberOfNames = 0;
    int index = 0;

    FileReader freader = new FileReader(new File("LastNames2000Census.txt"));
    Scanner inputFile = new Scanner(freader);

    while (inputFile.hasNext()) {
        inString = inputFile.nextLine();
        String[] nextString = inString.split(",");
        String nextTitle = nextString[0];
        int nextRank = Integer.parseInt(nextString[1]);
        int nextNumOfOccurrences = Integer.parseInt(nextString[2]);
        double nextProp100k = Double.parseDouble(nextString[3]);
        double nextCumProp100k = Double.parseDouble(nextString[4]);
        nameList[index] = new NameInfo(nextTitle, nextRank, nextNumOfOccurrences, nextProp100k, nextCumProp100k);
        index++;
    }

array is sorted and searched with insertion and binary generic methods. The error happens in the first line in the print statement:

do {
        ArraySort.insertionSort(nameList);
        boolean found = false;

        System.out.println("Hello, enter a last name.");
        selectedName = keyboard.nextLine();
        ArraySearch searcher = new ArraySearch(nameList);
        if (searcher.binarySearch(nameList, 0, 0, selectedName) == -1)
            System.out.println(selectedName + " was not found. Try again.");
        else {
            System.out.println(
                    selectedName + " was found at index " + index + "\n ranked: " + nameList[index].getRank()
                            + "/n number of occurences: " + nameList[index].getNumOfOccurrences()
                            + "\n proportion per 100,000 people: " + nameList[index].getProp100k()
                            + "\n cumulative proportion per 100,000 people: " + nameList[index].getCumProp100k());
        }
        System.out.println("Want to search another name? (Y/N)");
        anotherName = keyboard.nextLine();
    } while (anotherName.charAt(0) == 'y' || anotherName.charAt(0) == 'Y');

}

}

  • Pay attention at value set to `index` variable, for example add a `System.out.println(index)` before each iteration. – freedev Apr 06 '17 at 22:40
  • I think you should use the return value of the `binarySearch` method as `index` – Jorj Apr 06 '17 at 22:44

4 Answers4

0

An ArrayIndexOutOfBoundsException means you are accessing an offset in an array that isn't valid. Whatever offset you're looking up on that line is out of bounds. Now, why is index not a valid offset?

You incremented index after the last time you added something to the array, so it's no longer a valid offset.

nitind
  • 19,089
  • 4
  • 34
  • 43
0

Just ensure that the length of your array nextString of string is greater than your index i, before trying to access the element nextString[i]. ... if(i> nextString.length) ... = nextString[i] ;

0

I have fixed the out of bounds exception(thank you), but the program doesn't seem to be iterating through the file. When it runs, I only get the data from the first line of the file, no matter what name I type in:

while (inputFile.hasNext()) {
        inString = inputFile.nextLine();
        String[] nextString = inString.split(",");
        String nextTitle = nextString[0];
        int nextRank = Integer.parseInt(nextString[1]);
        int nextNumOfOccurrences = Integer.parseInt(nextString[2]);
        double nextProp100k = Double.parseDouble(nextString[3]);
        double nextCumProp100k = Double.parseDouble(nextString[4]); 
        nameList[index] = new NameInfo(nextTitle, nextRank, nextNumOfOccurrences, nextProp100k, nextCumProp100k);
        index++;
    }
    do {
        ArraySort.insertionSort(nameList);
        boolean found = false;

        System.out.println("Hello, enter a last name.");
        selectedName = keyboard.nextLine();
        ArraySearch searcher = new ArraySearch(nameList);
        index = searcher.binarySearch(nameList, 0, 0, selectedName);
        if (index == -1)
            System.out.println(selectedName + " was not found. Try again.");
        else {
            System.out.println(
                    selectedName + " was found at index " + index + "\n ranked: " + nameList[index].getRank()
                            + "\n number of occurences: " + nameList[index].getNumOfOccurrences()
                            + "\n proportion per 100,000 people: " + nameList[index].getProp100k()
                            + "\n cumulative proportion per 100,000 people: " + nameList[index].getCumProp100k());
        }
        System.out.println("Want to search another name? (Y/N)");
        anotherName = keyboard.nextLine();
    } while (anotherName.charAt(0) == 'y' || anotherName.charAt(0) == 'Y');

}

}

-1

Try this:

index = searcher.binarySearch(nameList, 0, 0, selectedName);
if (index == -1)
    System.out.println(selectedName + " was not found. Try again.");
else {...}
Jorj
  • 1,291
  • 1
  • 11
  • 32