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?