### テキストファイルを読み、単語数を出力する。単語はすべて小文字に変換処理する。 カウント数が多い単語順に出力することはできましたが、小文字変換することができません。 toLowerCaseで変換しようと試したのですがうまくいきません。 ```ここに言語を入力 public class TextCounter { static String FILE = "c:\p001-input.txt"; static String SEPARATOR = "(\s+?|\.|,|;)"; public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); try (FileReader f = new FileReader(FILE); BufferedReader br = new BufferedReader(f)) { String line; while ((line = br.readLine()) != null) { String[] words = line.split(SEPARATOR); for (String word : words) { if (!word.isEmpty()) { if (map.containsKey(word)) { int count = map.get(word) + 1; map.put(word, count); } else { map.put(word, 1); } } } } } catch (FileNotFoundException e) { System.out.println("ファイルが見つかりません"); } catch (IOException e) { System.out.println("読み込み失敗"); } List<String> list = new ArrayList<>(); int max = 0; for (String key : map.keySet()) { list.add(key); if (max < key.length()) { max = key.length(); } } Collections.sort(list, (o1, o2) -> { return - map.get(o1) + map.get(o2); }); String format = "%-" + max + "s: %3d"; for (String word : list) { int count = map.get(word); if (10 <= count) { System.out.printf(format, word, count); System.out.println(); } } } }
回答2件
あなたの回答
tips
プレビュー