Scanning a string character by character.
Loop with charAt to inspect each character; count matches or detect presence.
Character scanning visits every character in a String using an indexed loop. At each position, charAt(i) returns the character, which can then be tested or counted.
Common scanning tasks: count vowels, count letters, find positions, detect presence. The pattern is identical to array traversal but uses length() and charAt(i) instead of length and arr[i].
On the AP exam, character scanning is in nearly every String FRQ.
Loop with index; charAt(i) at each step.
Standard String traversal idiom.
Count vowels
public int countVowels(String s) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
count++;
}
}
return count;
}
Count target character
public int countChar(String s, char target) {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == target) count++;
}
return count;
}
Find first position (manual indexOf)
public int findChar(String s, char target) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == target) return i;
}
return -1;
}
Equivalent to s.indexOf(target). Both scan left-to-right and return the first match.
Why == on chars
Chars are primitives. == compares their values directly. .equals() is for object types (including String — but not char).
Scanning vocabulary.
Used in text-processing problems.
- Character scanning — visiting each char of a String.
- charAt(i) — access char at index i.
- length() — total chars in String.
- indexOf — built-in first-match search.
- Linear search — sequential scan until found.
- Sentinel return — -1 when not found.
How character scanning is tested.
Loop bound and char comparison dominate.
- Loop to
length(). Exclusive — last valid index islength - 1. - charAt for access. Bracket notation doesn't work.
- == for chars. equals() for Strings.
- Use
||for "any of these chars". Vowel checks chain conditions.
Trace countVowels("Education").
Lowercase only for this version.
Input: "Education" (note capital E).
- 'E' at 0: not in lowercase list → skip.
- 'd' at 1: skip.
- 'u' at 2: vowel → count = 1.
- 'c' at 3: skip.
- 'a' at 4: vowel → count = 2.
- 't' at 5: skip.
- 'i' at 6: vowel → count = 3.
- 'o' at 7: vowel → count = 4.
- 'n' at 8: skip.
Answer: 4. To also catch 'E' (uppercase), extend the condition or use Character.toLowerCase(c) first.
Scanning pitfalls.
Most are char-vs-String confusion.
- Using
.equals()on chars. Compile error. - Loop to
length() - 1. Misses the last char. - Bracket access.
s[i]doesn't compile. - Single quotes vs double. 'a' is char; "a" is String.
- Case mismatch. 'a' and 'A' are different.
Scanning template.
Standard loop structure.
Character Scanning Reference
AP Quick Reference| Element | Form | Note |
|---|---|---|
| Loop | for (int i = 0; i < s.length(); i++) | Standard. |
| Access | s.charAt(i) | Returns char. |
| Compare | c == target | Primitive equality. |
| Count | Increment in match branch | Initialize at 0. |
How to master character scanning.
Loop, charAt, compare with ==.
- MCQ Practice — for each option, verify the loop bound is
< length()and char comparison uses ==. - Java Lab — write countVowels, countDigits, findFirst. Test on mixed-case strings.
- FRQ Practice — the standard scan loop is muscle memory; rebuild it from scratch each time.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 12 Topic 05 Character Scanning and indexOf FRQ Practice
AP-style topic practice assessment