Validating input with String methods.
Checking length, comparing values, and accepting or rejecting input — the foundation of every login, form, or guard.
Validation answers a yes/no question about an input: is it the right length? Does it match an expected value? The core String methods for AP CSA validation are length(), equals(), and equalsIgnoreCase().
Validation methods return boolean and typically combine several checks with && (all must pass) or || (at least one must pass).
On the AP exam, validation appears in any FRQ that accepts user input — passwords, codes, names, IDs.
Combine length and content checks.
Each method answers one question; && combines them.
The three core methods
s.length()— number of characters ins.s.equals(t)— true ifsandthave the same characters in the same order, case-sensitive.s.equalsIgnoreCase(t)— same as equals but ignores letter case.
Password validation
public boolean isValidPassword(String password, String correctPassword) {
return password.length() >= 8 && password.equals(correctPassword);
}
Two checks: minimum length AND content match. Both must pass.
Why .equals(), never ==
Strings are objects in Java. == compares whether two variables point to the same object — not whether they have the same contents. "hi" == "hi" can be true or false depending on how the strings were created; "hi".equals("hi") is always true.
Null safety
If you call password.length() when password is null, you get a NullPointerException. Guard with a null check first if input might be null.
Validation vocabulary.
Standard string-handling terms.
- length() — number of characters in a String.
- equals() — exact content match (case-sensitive).
- equalsIgnoreCase() — content match ignoring case.
- Validation — checking input meets criteria.
- Predicate — boolean-returning helper.
- Reference equality — what
==tests; rarely what you want.
How validation is tested.
.equals() vs == is the dominant trap.
- Use
.equals()for String comparison. Never==. - Combine with
&&. All conditions must pass for valid input. - Length check first. Often a cheaper early-exit.
- Match return type. Validation returns
boolean.
Validate a 6-digit code.
Length and content together.
Problem. Accept a code if it's exactly 6 characters long and matches the expected code (case-insensitive).
public boolean isValid(String input, String expected) {
return input.length() == 6 && input.equalsIgnoreCase(expected);
}
Trace:
- isValid("ABC123", "abc123") → length is 6 ✓; equalsIgnoreCase ✓ → true.
- isValid("abc123", "abc123") → length is 6 ✓; equals ✓ → true.
- isValid("abc12", "abc123") → length is 5 ✗ → false; short-circuits, never checks content.
- isValid("xyz123", "abc123") → length is 6 ✓; equalsIgnoreCase ✗ → false.
Validation pitfalls.
== is the all-time top mistake.
==instead of.equals(). Compares references, not contents.- Wrong length comparison.
== 6requires exactly 6;>= 6allows longer. - Using
||instead of&&. Lets invalid input through. - Calling methods on null.
NullPointerException.
Validation methods at a glance.
Three methods; combine with &&.
Validation Reference
AP Quick Reference| Method | Returns | Use for |
|---|---|---|
length() | int | Length check. |
equals() | boolean | Case-sensitive match. |
equalsIgnoreCase() | boolean | Case-insensitive match. |
| Combine | && | All must pass. |
How to master validation.
.equals() always; combine with &&.
- MCQ Practice — for each option, flag
==on Strings as wrong immediately. - Java Lab — write isValidUsername, isValidPassword, isValidEmail. Combine length and content tests.
- FRQ Practice — return a boolean built from chained
&&conditions.
Practice — attempt these now.
AP-style assessments aligned to this lesson. Time them.
AP CSA Unit 12 Topic 01 String Methods and Validation FRQ Practice
AP-style topic practice assessment