-2

Hello I am building a Java program that takes in a file called wordlist which contains all of the words in the dictionary, one per line, and I pass it in through a scanner so that later I can use algorithms to search through it:

      File file = new File("wordlist.txt");

      Scanner scanner = new Scanner(file);

      String [] words = new String[69903];

No problem here. Then I try to populate the words array using a for loop:

      for(int i = 0; scanner.hasNextLine(); i++) {
      String input = scanner.nextLine();
      words[i] = input;
      }

This is where the problem is. I tried printing input every iteration of the loop and its an actual word from the .txt file. I also tried printing words[i] during the loop for each iteration and it also worked. Yet somehow when I iterate through the array after the loop it is full of nulls which obviously creates problems for the program down the line. What is happening and how can I fix it. Thank you.

By the way here is the entire program:

    import java.util.Scanner;
    import java.util.Arrays;
    import java.io.File;

    public class BinarySearch {

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

    File file = new File("wordlist.txt");

    Scanner scanner = new Scanner(file);

    String [] words = new String[69903];

    // Something here not working

    for(int i = 0; scanner.hasNextLine(); i++) {
    String input = scanner.nextLine();
    words[i] = input;
    }

    System.out.println("Done reading input words");

    System.out.println("Done sorting input words");

    Scanner query = new Scanner(System.in);

    String key = query.nextLine();

    if(search(key, words, 0, words.length) < 0) {

    System.out.println(key + " is not in the english dictionary.");

    } else {

    System.out.println(key + " is part of the english dictionary.");

    }

    }

    public static int search(String key, String[] arr, int lo, int 
    hi) {

    if(hi <= lo) {

    return -1;

    }

    int mid = lo + (hi - lo) / 2;

    int compute = arr[mid].compareTo(key);

    if (compute > 0) {

    return search(key, arr, lo, mid);

    }

    if (compute < 0) {

    return search(key, arr, mid + 1, hi);

    }

    else {

    return mid;

    }

    }




    }

This is the error I get:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 69903 at BinarySearch.main(BinarySearch.java:19)

gamerage3
  • 9
  • 5
  • 1
    You've got a bug in code not shown, perhaps you're shadowing the words array -- but who knows? -- making it very hard for us to help. You need to post a valid [mcve] for this to be answerable. – Hovercraft Full Of Eels May 15 '18 at 02:12
  • Ok I will post the whole thing – gamerage3 May 15 '18 at 02:12
  • Didn't request the whole thing but rather (and again) a valid [mcve]. Please do read the link before posting. – Hovercraft Full Of Eels May 15 '18 at 02:14
  • 2
    Does the file contain 69903 lines? If not, there'll be some nulls at the end of the array. Or do you mean there are _only_ nulls in the array? – Dawood ibn Kareem May 15 '18 at 02:14
  • Also, do you use `scanner` at all _between_ the two snippets you've shown? Maybe you've already put its internal pointer to the end of the file. – Dawood ibn Kareem May 15 '18 at 02:16
  • The array is only nulls – gamerage3 May 15 '18 at 02:18
  • Never mind and thank you so much ibn kareem. The file wordlist had 5 empty lines at the end. I deleted those and everything seems to work now. – gamerage3 May 15 '18 at 02:20
  • `java.lang.ArrayIndexOutOfBoundsException: 69903` has nothing to do with whether or not your array is full of nulls. It means you're accessing the 69903th element which doesn't exist. You created an array as `[69903]` so valid indexes are 0 to 69902. Your `search()` takes a `hi` index to which you're giving a length, whereas the initial high index should most likely be `length - 1`. – antak May 15 '18 at 02:38
  • @antak - Yeah, he/she changed the question. First it was "the array is full of nulls", second, it was the ArrayIndexOutOfBoundsException. Either way, it's bound to be a duplicate of something. – Dawood ibn Kareem May 15 '18 at 02:48

1 Answers1

0
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 69903 at BinarySearch.main(BinarySearch.java:19)

Do you have more than 69903 lines in the file or one extra empty line?

if(search(key, words, 0, words.length) < 0) 

Try to put 'i' instead of 'words.length'. Otherwise you'll process all the array including nulls after your last word. It will only work if your file contains exactly 69903 lines.

vadim
  • 178
  • 1
  • 9