Hi,
I'm trying to read a file, line by line, into an arrayList, assigning a different line to each element and ensure that:
1. It's actually happening (which is the reason for the println statements)
2. that the scope of the arrayList with the string elements filled in is sufficient so I can write a getter method like the one mentioned at the end of the wordList problem
The problem is, that it doesn't seem to be working, and if it was working I'm not sure how I would know.
File: prog4.java
package prog4;
import java.util.Scanner;
public class Prog4 {
public static void main(String[] args) {
WordList list = new WordList("states.txt");
list.readFile();
}
}
File: WordList.Java
package prog4;
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class WordList { // Class WordList is used to read in and store the list of words to be placed in the puzzle.
private String filePath;
// It has a single constructor with a single parameter containing the name of the file containing the word list. The file name should be stored in a local variable.
public WordList(String filePathParam){filePath=filePathParam;}
// The class has a method public boolean readFile() which reads in the list of
public boolean readFile() throws FileNotFoundException {
Scanner s = new Scanner(new File(filePath));
ArrayList<String> list = new ArrayList<String>();
while (s.hasNext()) {
list.add(s.next());
}
s.close();
for (int i = 0; i <= list.size(); i++) {
System.out.println(list.get(i));
}
return true;
} //end main method
//· The class has another method public Word[] getList() which returns the list of words as an array or class Word.
}