简体   繁体   中英

TreeSet not printing anything

I am trying to receive a file from my java directory, gather all the words in the document, put all the words into a TreeSet , and then print out the entire TreeSet of words. When I try the program, All that prints out from the TreeSet in the console is

Input file: 
trees.docx
[] 

It just ends with these empty brackets.Note: Inside the trees.docx file is only the words "trees and stuff". Here is my code:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class CountWords {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner sc = new Scanner(System.in);
        System.out.println("Input file: ");
        String fileName = sc.next();
        File inputFile = new File(fileName);
        Scanner in = new Scanner(inputFile);
        Set<String> words = new TreeSet<String>();

        // only happens if there is a next string
        while(in.hasNext()){
            words.add(in.next()); //adds this string to the treeSet initialized above
        }
        System.out.println(words); // prints the treeSet
    }
}

Java cannot read docx files. Either use external software to read Microsoft files or try another file type like .txt.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM