Menu
Coddy logo textTech

단어 세기

Coddy Java 여정의 논리와 흐름 섹션에 포함된 레슨 — 59개 중 52번째.

HashMap을 사용하여 단어 빈도를 계산하겠습니다. 단어가 키가 되고, 그 빈도가 값이 될 것입니다.

HashMap<String, Integer> wordCount = new HashMap<>();
String word = "hello";
wordCount.put(word, wordCount.getOrDefault(word, 0) + 1);
challenge icon

챌린지

쉬움

프로그램에 단어 카운팅 기능을 추가하세요. HashMap을 사용하여 단어 빈도를 저장하고 카운트하세요. 각 고유 단어의 카운트를 출력하세요.

예를 들어:

입력:

Coddy

예상 출력:

Word counts:
coddy: 1

직접 해보기

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String text = scanner.nextLine();
        
        String[] sentences = text.split("\\.");
        String[][] textArray = new String[sentences.length][];
        String[][] processedArray = new String[sentences.length][];
        
        for (int i = 0; i < sentences.length; i++) {
            textArray[i] = sentences[i].trim().split(" ");
            processedArray[i] = new String[textArray[i].length];
            
            for (int j = 0; j < textArray[i].length; j++) {
                if (!textArray[i][j].isEmpty()) {
                    processedArray[i][j] = textArray[i][j]
                        .replaceAll("[^a-zA-Z ]", "")
                        .toLowerCase();
                    System.out.println("Original[" + i + "," + j + "]: " + textArray[i][j]);
                    System.out.println("Processed[" + i + "," + j + "]: " + processedArray[i][j]);
                }
            }
        }
    }
}
quiz icon실력 점검

이 레슨에는 짧은 퀴즈가 포함되어 있습니다. 레슨을 시작해 문제를 풀고 진행 상황을 기록하세요.

논리와 흐름의 모든 레슨