Implementation
Turn the plan into code: normalize, tokenize, count, and print the top results with small, readable steps.
Implementation is a free Java Academy lesson on CoddyKit — lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Plan recap
Plan recap: lowercase, strip punctuation, split into tokens, count with a map, then print totals and top N. Keep each step small and clear.
Normalization
Normalization: convert to lowercase and replace non letters with spaces. This avoids separate tokens for Be vs be and removes commas and dots.
Tokenization
Tokenization: split on whitespace to get clean words. Trim first so the first token is not empty.
Counting
Counting: use a HashMap from word to count. For each token, increment the count or insert one if missing.
Reporting
Reporting: print total and unique counts, then list the top N words. For small texts, a simple selection loop is enough.
Code: full run
Run: you should see totals, unique count, and a top list. For large texts, replace selection with sorting or a min heap.
public class Main {
public static void main(String[] args) {
// Implementation: normalize, tokenize, count, and print top 5.
String text = "Be yourself; everyone else is already taken. Be kind. Be clear.";
// 1) Lowercase
String lower = text.toLowerCase();
// 2) Replace non letters with spaces
String cleaned = lower.replaceAll("[^a-z]+", " ");
// 3) Split on spaces and filter empties
String[] tokens = cleaned.trim().split("\\s+");
java.util.Map<String, Integer> freq = new java.util.HashMap<>();
int total = 0;
for (int i = 0; i < tokens.length; i = i + 1) {
String w = tokens[i];
if (w.length() == 0) continue;
total = total + 1;
Integer c = freq.get(w);
if (c == null) freq.put(w, 1);
else freq.put(w, c + 1);
}
// Print top 5 words using repeated selection (simple for small data)
int topN = 5;
for (int k = 0; k < topN; k = k + 1) {
String bestWord = null;
int bestCount = -1;
for (java.util.Map.Entry<String,Integer> e : freq.entrySet()) {
String word = e.getKey();
int count = e.getValue();
if (count > bestCount) {
bestCount = count;
bestWord = word;
}
}
if (bestWord == null || bestCount < 0) break;
System.out.println("#" + (k + 1) + " " + bestWord + " = " + bestCount);
// mark as used so it will not be picked again
freq.put(bestWord, -1000000);
}
System.out.println("Total words: " + total);
System.out.println("Unique words: " + (freq.size()));
}
}
Consistency check
Quick check: Which step is essential for consistent counting across cases and punctuation?
Recap
Recap: You implemented the pipeline end to end. Next, we will test with edge cases and consider simple extensions.
Frequently asked questions
Is the “Implementation” lesson free?
Yes — the full text of “Implementation” is free to read here on the web, and the Java Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.
What will I learn in “Implementation”?
Turn the plan into code: normalize, tokenize, count, and print the top results with small, readable steps. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Java Academy?
No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Implementation” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Java Academy lesson?
Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Requirements & Design
- Implementation
- Testing & Extensions