Java ScannerhasNext
メソッドを使用しようとしましたが、奇妙な結果が得られました。多分私の問題は非常に明白ですが、なぜこの単純な単純な表現が次の"[a-zA-Z']+"
ような単語に対して機能しないのですか:「points.anything,supervisor」. 私もこれを試し"[\\w']+"
ました。
public HashMap<String, Integer> getDocumentWordStructureFromPath(File file) {
HashMap<String, Integer> dictionary = new HashMap<>();
try {
Scanner lineScanner = new Scanner(file);
while (lineScanner.hasNextLine()) {
Scanner scanner = new Scanner(lineScanner.nextLine());
while (scanner.hasNext("[\\w']+")) {
String word = scanner.next().toLowerCase();
if (word.length() > 2) {
int count = dictionary.containsKey(word) ? dictionary.get(word).intValue() + 1 : 1;
dictionary.put(word, new Integer(count));
}
}
scanner.close();
}
//scanner.useDelimiter(DELIMITER);
lineScanner.close();
return dictionary;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}