[英]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. 我试图从我的java目录接收一个文件,收集文档中的所有单词,将所有单词放入
TreeSet
,然后打印出整个TreeSet
单词。 When I try the program, All that prints out from the TreeSet
in the console is 当我尝试该程序时,从控制台的
TreeSet
中打印出来的所有内容都是
Input file:
trees.docx
[]
It just ends with these empty brackets.Note: Inside the trees.docx file is only the words "trees and stuff". 它仅以这些空括号结尾。注意:trees.docx文件中只有单词“ 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. Java无法读取docx文件。 Either use external software to read Microsoft files or try another file type like .txt.
使用外部软件读取Microsoft文件,或尝试使用其他文件类型(例如.txt)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.