1

私の問題は、メイン メソッドを実行しても何も出力されないことです。私はHashSets にかなり慣れていないので、本当にばかげたことが原因で機能しないのではないかと心配しています。 dicはもともと an でしたが、効率のために sArrayListに変換しようとしています。HashSet

private Set<String>dic = new HashSet<String>(100000);
public void dictionary(){//reads/intializes arraylist dic from a file
    File data = new File("dictionaryForJava.txt");
    Scanner scanner=null;
    try {
        scanner = new Scanner(data);
        while (scanner.hasNextLine()) {
            dic.add(scanner.nextLine());
        }
    } catch (FileNotFoundException fnfe) {}
    if(scanner!=null)scanner.close(); // need if if file missing
}
public void print () throws IOException{
     Iterator it = dic.iterator();   
     while(it.hasNext())
         {
              String value =(String)it.next();
              System.out.println(value);  
         }
}
public static void main (String[] args)throws IOException{
    words test = new words();
    test.dictionary();
    test.print();
}
4

1 に答える 1

3

ファイルdictionaryForJava.txtが見つからない可能性は十分にあります。少なくともいくつかのデバッグ情報を出力せずに例外を食べてはいけません。これを試してください:

catch (FileNotFoundException fnfe) {fnfe.printStackTrace();}

ファイルの読み取りに問題があった場合、上記はコンソールに表示されます。

于 2013-03-14T21:34:32.973 に答える