So I did the part of putting it all into the array list, but I am not too sure as to how to go through the arraylist and look for any word that has a "d", my teacher says to use the IndexOf string function, and if its -1, there is no d, but if its more than -1, there is a d, so then I print it. When I run it, it is saying that this line (word = wordList.get(i);) is messed up. Here is what it says:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 170, Size: 170 at java.util.ArrayList.rangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at Challenge7.main(Challenge7.java:26)
import java.io.*;
import java.util.ArrayList;
public class Challenge7 {
public static void main(String[] args) {
int counter = 0;
String phrase, word;
ArrayList<String> wordList = new ArrayList<String>();
try {
FileReader in = new FileReader("Word List.txt");
BufferedReader readFile = new BufferedReader (in);
while ((phrase = readFile.readLine()) != null) {
System.out.println(phrase);
wordList.add(counter, phrase);
counter ++;
}
in.close();
readFile.close();
System.out.println("Done Reading....");
} catch (IOException e) {
System.out.println("Problem reading file.");
System.err.println("IOException: " + e.getMessage());
}
System.out.println("There are " + counter + " lines.\n");
for (int i = 0; i <= wordList.size(); i ++) {
word = wordList.get(i);
int index = word.indexOf('d');
if (index > -1) {
System.out.println(wordList.get(i));
}
}
}
}