Counting and Filtering Chars
Practical char tasks.
Counting and Filtering Chars is a free Java Academy lesson on CoddyKit — lesson 4 of 4. 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 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Counting With a Loop
To count characters, keep a counter variable and add 1 each time a condition matches.
Start the counter at 0, loop over every char, and increase it when the char passes your test.
int count = 0;
for (char c : "hello".toCharArray()) {
count++;
}Counting Vowels
Here we count vowels by checking each char against the vowel set.
We lowercase the char first so both 'A' and 'a' are counted the same way.
public class Main {
public static void main(String[] args) {
String text = "Java Rocks";
int vowels = 0;
for (char c : text.toCharArray()) {
char low = Character.toLowerCase(c);
if (low == 'a' || low == 'e' || low == 'i' || low == 'o' || low == 'u') {
vowels++;
}
}
System.out.println(vowels);
}
}Counting Digits
To count how many digits appear in a String, use Character.isDigit inside the loop.
This skips letters and symbols automatically, so your counter only grows for numbers.
public class Main {
public static void main(String[] args) {
String text = "abc123x9";
int digits = 0;
for (char c : text.toCharArray()) {
if (Character.isDigit(c)) digits++;
}
System.out.println(digits);
}
}Counting Letters
Swap in Character.isLetter to count alphabetic characters instead.
The structure stays the same: loop, test, and increment. Only the condition changes.
public class Main {
public static void main(String[] args) {
String text = "a1 b2 c3";
int letters = 0;
for (char c : text.toCharArray()) {
if (Character.isLetter(c)) letters++;
}
System.out.println(letters);
}
}Counting a Specific Char
To count one specific character, compare each char with ==.
This counts how many times the letter 'a' appears in the text.
public class Main {
public static void main(String[] args) {
String text = "banana";
int aCount = 0;
for (char c : text.toCharArray()) {
if (c == 'a') aCount++;
}
System.out.println(aCount);
}
}Filtering Out Characters
Filtering means building a new String that keeps only chars you want.
Here we keep letters and digits, dropping spaces and punctuation, by appending to a result String.
public class Main {
public static void main(String[] args) {
String text = "a-b 1!2";
String clean = "";
for (char c : text.toCharArray()) {
if (Character.isLetterOrDigit(c)) clean += c;
}
System.out.println(clean);
}
}Using StringBuilder
Building a String with += in a long loop is slow because each step makes a new String.
StringBuilder is faster: append chars, then call toString() at the end.
public class Main {
public static void main(String[] args) {
String text = "H e l l o";
StringBuilder sb = new StringBuilder();
for (char c : text.toCharArray()) {
if (!Character.isWhitespace(c)) sb.append(c);
}
System.out.println(sb.toString());
}
}Removing Whitespace
A common filter drops spaces, tabs, and newlines using Character.isWhitespace.
Keep every char that is not whitespace to squeeze the text together.
public class Main {
public static void main(String[] args) {
String text = " J a v a ";
StringBuilder sb = new StringBuilder();
for (char c : text.toCharArray()) {
if (!Character.isWhitespace(c)) sb.append(c);
}
System.out.println(sb.toString());
}
}Filter Then Count
You can combine ideas: filter to a clean String, then measure its length.
Here we count only the letters by filtering and reading length().
public class Main {
public static void main(String[] args) {
String text = "a1b2c3";
StringBuilder sb = new StringBuilder();
for (char c : text.toCharArray()) {
if (Character.isLetter(c)) sb.append(c);
}
System.out.println(sb.length());
}
}Upper and Lower Counts
You can track several counters at once. Here we count uppercase and lowercase letters separately.
Each char goes through two independent checks, growing the matching counter.
public class Main {
public static void main(String[] args) {
String text = "JavaCode";
int upper = 0, lower = 0;
for (char c : text.toCharArray()) {
if (Character.isUpperCase(c)) upper++;
if (Character.isLowerCase(c)) lower++;
}
System.out.println(upper + " " + lower);
}
}Why Filtering Matters
Counting and filtering are everyday tasks: validating passwords, cleaning user input, or tallying character types.
The same loop-and-test pattern handles them all. Change only the condition to fit your goal.
if (Character.isDigit(c)) {
count++;
}Quick Check
Test your understanding of counting and filtering chars.
Recap
Counting uses a counter you increment when a char passes a test like isDigit or ==.
Filtering builds a new String keeping only wanted chars, ideally with StringBuilder for speed. Change the condition to count vowels, letters, digits, or remove whitespace.
Frequently asked questions
Is the “Counting and Filtering Chars” lesson free?
Yes — the full text of “Counting and Filtering Chars” is free to read here on the web, and the Java Academy course includes 4 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 “Counting and Filtering Chars”?
Practical char tasks. 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 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Counting and Filtering Chars” 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
- The char Primitive
- Character Helper Methods
- Looping Over Characters
- Counting and Filtering Chars