1

My method read and prints the file, but I am having trouble adding each word to the ArrayList dict.

The reader reads the file one char at a time, so what I have written adds each char to dict: [c,a,t,d,o,g] when I want [cat,dog]. The text file has the words on their own line; how can I distinguish them?

My code so far:

public static List Dictionary() {
    ArrayList <String> dict = new ArrayList <String>(); 

    File inFile = new File("C:/Users/Aidan/Desktop/fua.txt");   
    FileReader ins = null;

    try {
        ins = new FileReader(inFile);

        int ch;

        while ((ch = ins.read()) != -1) {
            System.out.print((char) ch);

            dict.add((char) ch + "");
        }
    } catch (Exception e) {
        System.out.println(e);
    } finally {
        try {
            ins.close();
        } catch (Exception e) {
        }
    }
    return dict;
}
Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226
Bugod i
  • 13
  • 4
  • suppose there are good examples and approaches to solve this issue. refer following [post](https://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/) – Rajith Pemabandu May 03 '17 at 00:49

4 Answers4

0

Check out the answer here which shows how to use Scanner to get words from a file: Read next word in java.

Instead of printing out the words, you'd want to append them to an ArrayList.

Community
  • 1
  • 1
kashkar
  • 633
  • 1
  • 6
  • 19
0

As the read method of the FileReader can only read a single character at a time and that's not what you want, then I would suggest you use a Scanner to read the file.

ArrayList<String> dict = new ArrayList<>(); 
Scanner scanner = new Scanner(new File("C:/Users/Aidan/Desktop/fua.txt"));
while(scanner.hasNext()){
     dict.add(scanner.next());   
}
Ousmane D.
  • 50,173
  • 8
  • 66
  • 103
0

You can wrap your FileReader in a BufferedReader, which has a readLine() method that will get you an entire line (word) at a time. readLine() returns null when there are no more lines to read.

Kevin Anderson
  • 4,388
  • 2
  • 10
  • 20
0

Please observe Java naming conventions, so readDictionary instead of Dictionary (which looks like a class name). Next, I would pass the fileName into the method (instead of hard-coding the path in your method). Instead of reinventing the wheel, I would use a Scanner. You can also use the try-with-resources instead of finally here (and the diamond operator). Like,

public static List<String> readDictionary(String fileName) {
    List<String> dict = new ArrayList<>();

    try (Scanner scan = new Scanner(new File(fileName))) {
        while (scan.hasNext()) {
            dict.add(scan.next());
        }
    } catch (Exception e) {
        System.out.printf("Caught Exception: %s%n", e.getMessage());
        e.printStackTrace();
    }
    return dict;
}

Alternatively, use a BufferedReader and split each word yourself. Like,

public static List<String> readDictionary(String fileName) {
    List<String> dict = new ArrayList<>();

    try (BufferedReader br = new BufferedReader(new FileReader(
                new File(fileName)))) {
        String line;
        while ((line = br.readLine()) != null) {
            if (!line.isEmpty()) {
                Stream.of(line.split("\\s+"))
                        .forEachOrdered(word -> dict.add(word));
            }
        }
    } catch (Exception e) {
        System.out.printf("Caught Exception: %s%n", e.getMessage());
        e.printStackTrace();
    }
    return dict;
}

But that is basically what the first example does.

Elliott Frisch
  • 183,598
  • 16
  • 131
  • 226