1

So, I have a program that is supposed to go through a file called dictionary.txt and check if the inputted word is inside the dictionary text file.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {


public static void main(String[] args){

    String word = null;
    Scanner scan = new Scanner(System.in);
    word = scan.nextLine();

    try {
        if(isInDictionary(word, new Scanner(new File("dictionary.txt")))){
            System.out.println(word + " is in the dictionary");
        } else System.out.println(word + " is NOT in the dictionary");
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static boolean isInDictionary(String word, Scanner dictionary){

    List<String> dictionaryList = new ArrayList<String>();
    for(int i = 0; dictionary.hasNextLine() != false; i++){
        ++i;
        dictionaryList.add(dictionary.nextLine());
        if(dictionaryList.get(i) == word){
            return true;
        }
    }

    return false;

}

}

When I try to run it I get this error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.get(ArrayList.java:411)
at io.github.mediocrelogic.checkDictionary.Main.isInDictionary(Main.java:34)
at io.github.mediocrelogic.checkDictionary.Main.main(Main.java:19)

Why am I receiving an IndexOutOfBoundsException here? There are no syntactic errors with the code. The dictionary.txt file is about 19.95mb, is that why I am receiving this exception?

Jason C
  • 38,729
  • 14
  • 126
  • 182

3 Answers3

4

If you remove the stray ++i in your loop, it should fix your issue.

for(int i = 0; dictionary.hasNextLine() != false; i++){
    //++i;  // <-- THIS SHOULD GO AWAY!
    dictionaryList.add(dictionary.nextLine());
    if(dictionaryList.get(i) == word){
        return true;
    }
}

You are already incrementing i in your for statement. By incrementing it again inside the loop, i goes past the end of the dictionary, and hence the exception.

By the way, please also see How do I compare strings in Java?, as you will not want to use == to compare strings there.

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182
  • Ah, so the dictionary list becomes twice as large then? Thanks! – user3479380 Mar 31 '14 at 14:48
  • @user3479380 No, it doesn't. But your index grows twice as fast as the dictionary, and so you are guaranteed to get an out-of-bounds exception when trying to access that index in the dictionary. I've removed that statement because it was meant to be explanatory but it seems it misled you. – Jason C Mar 31 '14 at 17:05
  • @user3479380 You may wish to read the documentation for [`ArrayList.get()`](http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#get(int)), which clearly explains what that exception means. – Jason C Mar 31 '14 at 17:12
4

Please remove the line of code ++i; entirely. i is already incremented in the for loop.

xagyg
  • 9,562
  • 2
  • 32
  • 29
2
for(int i = 0; dictionary.hasNextLine() != false; i++){
    ++i;

After that code your counter increase twice but the Index of the ArrayList just increased by one after this

dictionaryList.add(dictionary.nextLine());

That means you always trying to get an item from the ArrayList With i that is equal to ArrayList Index+1

You should remove this ++i from you code and it's will work

Also you can get better way to search for word into txt file using Regex and matcher object

http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html

or Indexof for more quickness search

http://www.homeandlearn.co.uk/java/indexOf.html

Jason C
  • 38,729
  • 14
  • 126
  • 182
Antwan
  • 3,837
  • 9
  • 41
  • 62