Palindrome tester with Java, ignoring spaces and punctuation

I have the program made up until the point where it has to ignore and punctuations and spaces in the thread and I was wondering if anyone could help me with the coding for that? What I've been trying out doesn't seem to be working. Here is what I have so far:

import java.util.Scanner;

public class PalindromeTester
public static void main (String[] args)


    String str, another = "y";

    int left, right;

    char charLeft, charRight;

    Scanner scan = new Scanner (;

    while (another.equalsIgnoreCase("y")) // allows y or Y


        System.out.println ("Enter a potential palindrome: ");

        str = scan.nextLine();

        left = 0;

        right = str.length() - 1;

        while (left < right)
            charLeft = str.charAt(left);
            charRight = str.charAt(right);

            if (charLeft == charRight)

            else if (charLeft == ',' || charLeft == '.' ||
 charLeft == '-' || charLeft == ':' ||
 charLeft == ';' || charLeft == ' ')


            else if (charRight == ',' || charRight == '.' ||
 charRight == '-' || charRight == ':' ||
 charRight == ';' || charRight == ' ')




        if (left < right)
            System.out.println ("That string is NOT a palindrome.");

            System.out.println ("That string IS a palindrome.");


    System.out.print ("Test another palindrome (y/n)? ");

    another = scan.nextLine();


13.10.2009 23:56:39
Is this something you're doing for homework for a class?
Ben Torell 14.10.2009 00:06:32
When I built it an ran it using 'tyt' and 't,yt' it worked, what strings are you using?
Chris Thompson 14.10.2009 00:07:09
You may find it easier to check if a character is a letter or non-letter instead of explicitly calling out the punctuation like you're doing.
Carl Norum 14.10.2009 00:10:24

You could simplify the code significantly by removing all the spaces and punctuation before you start. Look at String.replaceAll(regex,replacement). You would write a regular expression to match blanks and punctuation, and provide an empty string ("") as the replacement. This will return a new string containing the original minus the characters you want to ignore.

14.10.2009 00:12:03
If what Ben Torrell suspects is true, regex's might be a wee above Lisa's head. You could accomplish the same thing with a bunch of calls to String.replace().
Chris Thompson 14.10.2009 00:13:28
Or she could determine whether or not a character is a letter or not with Character.isLetter(char).
Ben Torell 14.10.2009 00:17:53

Look at char's documentation entry. Specifically the isLetterOrDigit method. If that method returns false, then it's punctuation or a space. There are other methods in there as well that can help with that.

14.10.2009 00:17:38

Just to clarify what Jim Garrison said, the regex you need is the following

String m = "Madam, I'm'',.,.''   Adam";
m = m.toLowerCase().replaceAll("\\W", "");

This will leave only letters and digits and remove whitespace and punctuation, i.e. m will become "madamimadam" and you can run you regular palindrome test on that string.

You can learn more about regular expressions here

14.10.2009 03:42:12

Your Problem: You are not ignoring the case of the letters. So if you try Able was I, ere I saw Elba, it will not come back correctly, although it is a true palindrome.

14.03.2011 18:36:36

This is the programming assignment from the Java Software Solutions (PP3.11) that I assign my students. Ironically the teacher solution uses Character.isLetterOrDigit(___) (which is never mentioned in the book) and uses methods to get rid of spaces and punctuation (having not even taught methods at that point in the book), and char is not an official part of the AP CS subset. Silly publishers.

7.11.2012 08:49:53

This code for determine if a word is a palindrome can be much more simplified. Find updated Code

String word;
int z;
int y = 0;
int i = 0;

char letter;

Scanner input = new Scanner(;

System.out.print("Enter a word: ");
word = input.nextLine();

word = word.replaceAll("\\s+", "");
word = word.toLowerCase();

z = word.length()-1;
while (i <= z){

    if ((letter = word.charAt(i)) == (letter = word.charAt(z-i))){
        y += 1;
    i += 1;

if (y == (z+1)){
    System.out.println("The word IS a palindrome");
    System.out.println("The word is NOT a palindrome");

7.01.2016 07:56:24

This looks like a really old post but I think I stumbled upon a simpler solution for a palindrome test. This checks the first and last characters and moves inwards and exits the program as soon as the characters do not match.

public class CharTest {
    public static void main(String[] args) {
             //converts string to lowercase and replaces everything except numbers
             // and alphabets
        String s = "Niagara. O roar again!".toLowerCase().replaceAll("\\W", "");
        int j=0;
        int k = s.length() - 1;
        while(j < s.length() / 2) { //loops until half the length of the string if 
                                        //even and floor value if odd.
            if (s.charAt(j++) != s.charAt(k--)){//check for first and last chars                                                                                                
                                              //and  go inwards. if char do not match print 'Not a Palindrome' and exit 
                System.out.println("Not a Palindrome");
        System.out.println("Palindrome");  //if every chars match print "Palindrome"
12.08.2013 20:40:35