Validating Strings in Java
Swipe to show menu
When working with text input, users rarely give you exactly what you expect — some leave fields empty, some type only spaces, some forget required symbols. Because of that, before using any string in a program, we often need to validate it first — checking whether it follows certain rules.
We already saw basic tools like isEmpty(), length(), and contains(), but now we'll add a few more that are especially useful in real input validation scenarios.
isBlank()
A very common issue is when a string is not technically empty, but still contains only spaces. For example, a user might "type" a name but only hit the spacebar a few times. .isBlank() handles this by treating whitespace-only strings as invalid input. It returns a boolean.
Main.java
123456789101112package com.example; public class Main { public static void main(String[] args) { String customerName = " "; boolean isBlankName = customerName.isBlank(); System.out.println(isBlankName); } }
Even though customerName is not completely empty, it contains only spaces — so .isBlank() returns true, signaling that there is no meaningful content.
startsWith()
Sometimes we need to ensure a string follows a specific structure. For example, every order code in our system might need to start with "ORD". .startsWith() checks the beginning of the string and returns a boolean.
Main.java
123456789101112package com.example; public class Main { public static void main(String[] args) { String orderCode = "ORD-2459"; boolean validOrderCode = orderCode.startsWith("ORD"); System.out.println(validOrderCode); } }
.startsWith("ORD") checks the first three characters of orderCode — since they match, it returns true, confirming the code follows the expected format.
endsWith()
We can also validate the end of a string — especially useful for file names or formats. For example, ensuring a receipt file is actually a PDF. It returns a boolean.
Main.java
123456789101112package com.example; public class Main { public static void main(String[] args) { String receiptFile = "receipt.pdf"; boolean validReceiptFile = receiptFile.endsWith(".pdf"); System.out.println(validReceiptFile); } }
.endsWith(".pdf") checks the last four characters of receiptFile — since they match ".pdf", it returns true.
Combined Validation
Real validation is usually not just one check — it's multiple rules working together. For example, validating a username might require that it is not blank, has at least 5 characters, and contains no spaces:
Main.java
123456789101112131415package com.example; public class Main { public static void main(String[] args) { String username = "coffeeMaster"; boolean validUsername = !username.isBlank() && username.length() >= 5 && !username.contains(" "); System.out.println(validUsername); } }
All three conditions are combined with && — the username must pass every single check to be considered valid. "coffeeMaster" is not blank, has more than 5 characters, and contains no spaces, so validUsername is true.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat