![](/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.