A Palindrome Checker is a fun, practical Java project for beginners that introduces string manipulation, logic, user input, and clean problem-solving. A palindrome is a word, phrase, or number that reads the same forwards and backwards (e.g., "madam", "racecar", "121"). Here’s how to build a console-based palindrome checker in Java, step by step.
What Will This Palindrome Checker Do?
- Ask the user to enter a word, number, or phrase.
- Tell the user if the input is a palindrome or not.
- Optionally, ignore case and non-letter characters for more advanced checking.
Step 1: Java Concepts Practiced
-
User input: Using the
Scanner
class. - String manipulation: Reversing strings, comparing values, ignoring case.
-
Control flow: Using
if-else
statements. - Methods: Structuring the program for clarity and reuse.
Step 2: Import the Scanner Class
java
import java.util.Scanner;
Step 3: Basic Program Structure
Define the main class and entry point:
java
public class PalindromeChecker {
public static void main(String[] args) {
// Main logic goes here
}
}
Step 4: Read Input from the User
Prompt the user for a word, number, or phrase. Use Scanner
to read the input as a full line.
java
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word, number, or phrase: ");
String input = scanner.nextLine();
Step 5: Clean the Input (For Advanced Checking)
(Optional) If you want to allow palindromes with spaces and ignore case (e.g., "A man, a plan, a canal, Panama"), clean the string:
java
String cleanedInput = input.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
-
replaceAll("[^a-zA-Z0-9]", "")
: Removes all non-letter and non-digit characters. -
toLowerCase()
: Makes comparison case-insensitive.
Step 6: Reverse the String
You can use a simple loop, but a quick way is to use StringBuilder:
java
String reversed = new StringBuilder(cleanedInput).reverse().toString();
Step 7: Check for Palindrome
java
if (cleanedInput.equals(reversed)) {
System.out.println("It's a palindrome!");
} else {
System.out.println("Not a palindrome.");
}
Step 8: Full Code Example – Palindrome Checker in Java
Here’s the complete, extendable code that checks palindromes for words, numbers, and phrases, ignoring spaces and punctuation:
java
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a word, number, or phrase: ");
String input = scanner.nextLine();
// Remove non-alphanumeric and convert to lowercase
String cleanedInput = input.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();
// Reverse the cleaned input
String reversed = new StringBuilder(cleanedInput).reverse().toString();
if (cleanedInput.equals(reversed)) {
System.out.println("It's a palindrome!");
} else {
System.out.println("Not a palindrome.");
}
scanner.close();
}
}
Explanation of Key Parts
- User Input: Reads a full line to allow for spaces and punctuation.
- String Cleaning: Makes the check case-insensitive and punctuation-agnostic by stripping out all non-letter, non-number characters.
-
Reversing the String: Uses
StringBuilder
for quick and efficient reversal. - Comparison: Checks if the cleaned version and its reverse are identical.
- Output: Tells the user instantly if the input is a palindrome.
Running the Program
1.Copy the code to a file named PalindromeChecker.java
.
2.Compile:
text
javac PalindromeChecker.java
3.Run:
text
java PalindromeChecker
Enter any word, number, or phrase—for example, try:
madam
Racecar
A man, a plan, a canal, Panama
12321
Ideas for Expansion
- Check multiple inputs in a loop until the user types "exit".
- Make it case sensitive by skipping the
toLowerCase()
step. - Only allow word-level palindrome checking (skip phrases).
- Add detailed output explaining why input is (or isn't) a palindrome.
- Build a simple GUI version with JavaFX or Swing.
Why This Project is Perfect for Beginners
- Short and clear: The code is easy to follow and change.
- String skills: Teaches valuable string handling and comparison methods.
- Logic practice: Encourages thinking about input variations and algorithm steps.
- Expandable: Offers great opportunities for further learning and customization.
By building a Palindrome Checker, you solidify Java fundamentals and gain the confidence to tackle more complex projects. Happy coding!
Check out the YouTube Playlist for great java developer content for basic to advanced topics.
Please Do Subscribe Our YouTube Channel for clearing programming concept and much more ... : CodenCloud
Top comments (0)