0

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));
            }
        }
    }

}
Rohan D
  • 1
  • 3
  • 3
    I believe it is because of your for loop, an array or list is zero indexed, so trying to pull back wordList.get(i) when i == the size() would cause an index out of bounds error – Ryan Wilson Feb 15 '18 at 19:39
  • _"It says there is no such thing"_ -- No, it doesn't. It gave you a specific error message, and we need to see the _actual_ message, not your interpretation of the message. Please visit the [help] and read [ask] to learn how to use this site effectively. – Jim Garrison Feb 15 '18 at 19:39
  • @RohanD Try changing your for loop to int i = 0; i < wordList.size(); i++ and see if you still receive the error, if so, please post the exact error as Jim said. – Ryan Wilson Feb 15 '18 at 19:41
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – lucidbrot Feb 15 '18 at 22:21

1 Answers1

0

Simple fix; turn

(int i = 0; i <= wordList.size(); i ++)

into

(int i = 0; i < wordList.size(); i ++)

in the for loop. When you do <=, it gets element 170, which doesn't exist. There are only 0 - 169, but by calling size, its trying to grab the next nonexistent word.

Here's a better example:

When you run the code with 5 words, this is what the arraylist will read it in as:

0 Hi
1 My
2 Name
3 Is
4 Sean

Notice that the number that is at the largest index is 4, while the size of the arraylist overall is 5. Because the index starts at zero, you only want to search the arraylist until you reach the one right before the arraylist; as soon as you reach the size, you've gone outside the index, causing an "Index Out Of Bounds" Error.

Sean
  • 168
  • 7