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.
}

Recommended Answers

All 8 Replies

what do you mean, it's not working? are you getting an error message?
I would take a slightly different approach, but I don't see anything wrong with your code (in a first glance, anyway)

how do you know it's not working?

Try to add some System.out.println in the readFile method and see what happens. Also you need to declare the list as a private member of the class like filePath. With your way, you create it in the method readFile and then after it executes, you have lost it, you cannot access it

included is some updates to the code:
File Prog4.java:

package prog4;
import java.util.Scanner;
public class Prog4 {
public static void main(String[] args) {
    WordList list = new WordList("states.txt");
    try{list.readFile();} catch (Exception ex) { }
    list.TestThingsOut();
}
}

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
    private ArrayList<String> list = new ArrayList<String>();

    public boolean readFile() throws FileNotFoundException {
        Scanner s = new Scanner(new File(filePath));

        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 readFile method

    public void TestThingsOut() {
        for (int i = 0; i <= list.size(); i++) {
            System.out.println(list.get(i));
        }
    }//end TestThingsOut method
//· The class has another method public Word[] getList() which returns the list of words as an array or class Word
    public Word[] getList() {
        Word[] word = null;
        return word;
    } //end getList method
} //end class WordList

stultuske,there are no errors of a compile or run-time nature that I could find, the problem is that I don't seem to have access assigned to the arrayList in the readFile() method (not even from a getter in the same class), and I am unable to test that it works

javaAddict, I've given that a shot, it looks like I still can't access the variable, I've included a new testing method TestThingsOut and an updated arrayList declaration (location and now a private variable), but still no output to confirm I have access - the program runs without any errors, I just don't know why it isn't working

what do you mean 'access assigned'? sure, you read the values and store them in an ArrayList, but you don't do anything with that ArrayList.

What I'm saying is, I'm trying to see if I can access the values assigned to arrayList in the readFile method from outside readFile when the method is done executing. When I wrote "access assigned," I mean accessing the strings assigned to ArrayList within the readFile method from outside the method once readFile was done executing.

add a System.out.println(ex.getMessage());
in the catch of the try-catch block

What I'm saying is, I'm trying to see if I can access the values assigned to arrayList in the readFile method from outside readFile when the method is done executing. When I wrote "access assigned," I mean accessing the strings assigned to ArrayList within the readFile method from outside the method once readFile was done executing.

well yes of course you can access the array list, as long as it is in scope/ was declared globally, or has a return variable that returns the array list after the method...

Ok, that turned out to work for me. I've got past that problem now. Thank You.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.