0

I am trying to read from a file and print the average word length for the ArrayList. When I print or get the average of the list was populated with the font and other material from the .txt file.

I have already created a file reader and here is the code for populating the ArrayList from file:

    String homedir = System.getProperty("user.home");
    System.out.println("homedir = " + homedir);
    JFileChooser jfc = new JFileChooser(new File(homedir));
    jfc.showOpenDialog(null);
    File file = jfc.getSelectedFile();
    Scanner scanner = new Scanner(file);
    System.out.println("File read = " + file);
    ArrayList<String> words = copyWords(scanner);
    return words;

the copyWords method

    ArrayList<String> words = new ArrayList<String>();
    while (scanner.hasNext()){
        words.add(scanner.next().toLowerCase());
    }
    return words;

I understand that I need to convert it via toString() but how would I do that. I tried to do Arrays.toString(words) but that did not work. Would I have to write the copyWords method using toString() when I add the words?

I guess I am just confused about where my @Override needs to go if anywhere. I tried to search but don't understand how to get what I find to solve my problem.

  • .txt files shouldn't have font information, so your statement about this has me greatly confused. Please post more information about the file. You're not saving it using wordpad are you? – Hovercraft Full Of Eels Oct 21 '16 at 01:58
  • And this: `"I understand that I need to convert it via toString()"` doesn't make sense. What String output are you trying to achieve? Most of the time you *wouldn't* use ArrayList's toString() except if you want your words in the output that it produces. Else you'll likely need to iterate through the list, perhaps populating a StringBuilder, but how? that will depend on what you're trying to get. – Hovercraft Full Of Eels Oct 21 '16 at 02:00
  • I figured it out. It wss because the files were saving as .rtf not .txt so they were using font. I didn't think of it until you said ".txt files shouldn't have font" – Jack Schirtz Oct 21 '16 at 03:19

0 Answers0