![](/img/trans.png)
[英]ArrayIndexOutOfBoundsException while printing a TreeSet
[英]TreeSet not printing anything
我试图从我的java目录接收一个文件,收集文档中的所有单词,将所有单词放入TreeSet
,然后打印出整个TreeSet
单词。 当我尝试该程序时,从控制台的TreeSet
中打印出来的所有内容都是
Input file:
trees.docx
[]
它仅以这些空括号结尾。注意:trees.docx文件中只有单词“ trees and stuff”。 这是我的代码:
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无法读取docx文件。 使用外部软件读取Microsoft文件,或尝试使用其他文件类型(例如.txt)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.