creativethoughts
creativethoughts

Reputation: 47

Store .txt File in Arraylist

Here is my full code code to read a file:

public static void main(String[] args) {
    List <String> words = new ArrayList <String>();
    BufferedReader br = null;
    try {
        br = new BufferedReader(new FileReader("3LetterWords.txt"));
        String word;
        while ((word = br.readLine()) != null ){
            words.add(word);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    String [] wordList = new String[words.size()];
    words.toArray(wordList);
    List<String> combinations = display(new ArrayList<>());
}

public static  List<String> display(ArrayList<String> combinations) {
    int counter = 1;
    for (char c1 = 'a'; c1 <= 'z'; c1++) {
        for (char c2 = 'a'; c2 <= 'z'; c2++) {
            for (char c3 = 'a'; c3 <= 'z'; c3++) {
                String combo = "" + c1 + c2 + c3;                    
                combinations.add(combo);
                System.out.println("" + counter++ + " " + c1 + c2 + c3);
            }
        } 
    }
    return combinations;
}

When I run the program, I get an error in the output that I'm not sure how to read or interpret (in NetBeans). I want the .txt file to be read, then stored into the ArrayList: The ArrayList is created to hold all the information from the file. Use an ArrayList so that you don't have to know the size of the file.

The BufferedReader commands locate the file and open it for reading.

The while loop reads information in line-by-line until it sees the “null” or “end of file” character. It then adds each entry to the ArrayList.

The catch section makes sure you don't read past the end of the file.

The second try section closes the file.

The last function is to convert the ArrayList to an array, so that you can search through it.

Added full code: For context: I need to use a .txt file of 3 letter words for sort/search methods used between both array lists.

ERROR:

java.io.FileNotFoundException: 3LetterWords.txt (No such file or directory)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:216)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:111)
    at java.base/java.io.FileReader.<init>(FileReader.java:60)
    at wordleproj.WordleProj.main(WordleProj.java:17)
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.BufferedReader.close()" because "br" is null
    at wordleproj.WordleProj.main(WordleProj.java:26)
/Users/mac/Library/Caches/NetBeans/12.6/executor-snippets/run.xml:111: The following error occurred while executing this line:
/Users/mac/Library/Caches/NetBeans/12.6/executor-snippets/run.xml:68: Java returned: 1

Upvotes: 0

Views: 89

Answers (1)

xarcher
xarcher

Reputation: 304

You need to create a file 3LetterWords.txt in your project or add file path.

// you don't have the path but only the filename so the file should be in your project.
br = new BufferedReader(new FileReader("3LetterWords.txt"));

OR specify the path

br = new BufferedReader(new FileReader("C:\\Users\\Admin\\Downloads\\3LetterWords.txt"));

Upvotes: 1

Related Questions