Hi guys so I was playing with some code which would determine the whether a string is a palindrome or not. Its been a mixed bag of results with some palindromes being detected while others aren't. I do apologize to the lack of polish of my comments but I hope that someone might be able to see where the error is and give me good feedback. I think the error is in the set of code that removes the special characters from the string.
As a reference, I am using this webiste: http://www.palindromelist.net/ as the source for the palindrome strings. However the really long ones have resulted in errors.

package palindrome;
/* 
 * Author: venom 
 * Last Edit Date: 2/15/2019
 * This program will check whether a string is a palindrome
 * JDK 11
 */

import java.util.Scanner; // Scanner class used to collect user input of various strings
import javax.swing.JOptionPane; // Will be used to build the GUI for the palindrome test

public class Palindrome {

    public static void main(String[] args) {
        palindrome();
    }

public static void palindrome(){
        //Create the a scanner object to collect user input However will be re-enabled at the end
        Scanner sc = new Scanner(System.in);

        //System.out.print();
        //Variable to store the user string
        String input = null;
        do {
       input = JOptionPane.showInputDialog(null,"Welcome to the Palindrome Test App.\n" + 
                                                    "Enter a string to test: ", "Palindrome App", 
                                                    JOptionPane.INFORMATION_MESSAGE);
        } while (input.isEmpty() == true);

        input = input.toLowerCase();// set the input to lower case 
        // Here we are removing all special characters from a string
        input = input.replace(" ","").replace("!", "").replace(".", "").replace(",", "").replace("?","")
                .replace("-", "").replace("/", "").replace("\\","").replace("'", "").replace("\"", "").replace(":", "")
                .replace("`", "").replace(";", ""); 

        String message = null;

        //String dummy1 = "Jabroni"; // This is the first dummy palindrome string
        //dummy1 = dummy1.toLowerCase();
       // String dummy2 = ""; // This a non palindrome string

        int strLength = input.length(); // This will count the number of characters in a string
        int midCount = strLength / 2; // The "midCount" is divided into half so that we do not loop all the way

        // Initialize the character variables used to compare the chars in the loop
        char bChar; // Will store to check the beginning character
        char eChar; // Will store to check the ending char

        // Initialize a counter variable which will be used to compare
        //System.out.println(dummy1);

        int cCount = 0; // This will count how many chars are similar
        for (int x = 0; x < midCount; x++)
        {

            strLength--; // Reduces the string by 1
            //System.out.println(strLength);
            bChar = input.charAt(x);
            eChar = input.charAt(strLength);

            // If conditionals for comparison of characters
            if (bChar == eChar){
               cCount += 1;
            } 
        }

        // Compare the midpoint with cCount 
        // If they match then the code is a success if not we have failed
        if (cCount == midCount){
            message = "Success! ;-D The string is a Palindrome";
        }
        else 
            message = "It is not a Palindrome. Sorry :-(";

        JOptionPane.showMessageDialog(null, message, "Palindrome App", JOptionPane.INFORMATION_MESSAGE);
    }     
}

Recommended Answers

All 8 Replies

have resulted in errors.

What errors exactly? Help us to help you.

commented: Yes. "Missing value on line 42" can really speed this along. +15

So the errors that arise is that some palindromic strings are not being detected for example, " Are we not pure? “No sir!” Panama’s moody Noriega brags. “It is garbage!” Irony dooms a man; a prisoner up to new era." which is clearly a palindrome is detected as not palindromic. I think I have removed all special characters that are in the string.

I have made a small change to the code regarding the string: Are we not pure? “No sir!” Panama’s moody Noriega brags. “It is garbage!” Irony dooms a man; a prisoner up to new era. And hence I print out the resulting string after the special character removal code.

package palindrome;
/* 
 * Author: venom 
 * Last Edit Date: 2/15/2019
 * This program will check whether a string is a palindrome
 * JDK 11
 */

import java.util.Scanner; // Scanner class used to collect user input of various strings
import javax.swing.JOptionPane; // Will be used to build the GUI for the palindrome test

public class Palindrome {

    public static void main(String[] args) {
        palindrome();
    }

public static void palindrome(){
        //Create the a scanner object to collect user input However will be re-enabled at the end
        Scanner sc = new Scanner(System.in);

        //System.out.print();
        //Variable to store the user string
        String input = null;
        do {
       input = JOptionPane.showInputDialog(null,"Welcome to the Palindrome Test App.\n" + 
                                                    "Enter a string to test: ", "Palindrome App", 
                                                    JOptionPane.INFORMATION_MESSAGE);
        } while (input.isEmpty() == true);

        input = input.toLowerCase();// set the input to lower case 
        // Here we are removing all special characters from a string
        input = input.replace(" ","").replace("!", "").replace(".", "").replace(",", "").replace("?","")
                .replace("-", "").replace("/", "").replace("\\","").replace("'", "").replace("\"", "").replace(":", "")
                .replace("`", "").replace(";", ""); 

        System.out.println(input);

        //String dummy1 = "Jabroni"; // This is the first dummy palindrome string
        //dummy1 = dummy1.toLowerCase();
       // String dummy2 = ""; // This a non palindrome string

        int strLength = input.length(); // This will count the number of characters in a string
        int midCount = strLength / 2; // The "midCount" is divided into half so that we do not loop all the way

        // Initialize the character variables used to compare the chars in the loop
        char bChar; // Will store to check the beginning character
        char eChar; // Will store to check the ending char

        // Initialize a counter variable which will be used to compare
        //System.out.println(dummy1);

        int cCount = 0; // This will count how many chars are similar
        for (int x = 0; x < midCount; x++)
        {

            strLength--; // Reduces the string by 1
            //System.out.println(strLength);
            bChar = input.charAt(x);
            eChar = input.charAt(strLength);

            // If conditionals for comparison of characters
            if (bChar == eChar){
               cCount += 1;
            } 
        }

        String message = null;
        // Compare the midpoint with cCount 
        // If they match then the code is a success if not we have failed
        if (cCount == midCount){
            message = "Success! ;-D The string is a Palindrome";
        }
        else 
            message = "It is not a Palindrome. Sorry :-(";

        JOptionPane.showMessageDialog(null, message, "Palindrome App", JOptionPane.INFORMATION_MESSAGE);
    }     
}

The resulting string is: arewenotpure“nosir”panama’smoodynoriegabrags“itisgarbage”ironydoomsamanaprisoneruptonewera
How do I remove the remaining special characters?

The double quote marks in your input string are open/close double curly quotes, not simple straight double quotes, so you do not remove them.

Thank you so much for all who commented. I couldn't let it go and I found a simple regular expression that would remove all non word characters and I replaced that with all the string.replace code. Finally!!! I guess I will be learning Regular expressions in the near future LOLOL.

I have attached the code below as:

package palindrome;
/* 
 * Author: venom 
 * Last Edit Date: 2/15/2019
 * This program will check whether a string is a palindrome
 * JDK 11
 */

import java.util.Scanner; // Scanner class used to collect user input of various strings
import javax.swing.JOptionPane; // Will be used to build the GUI for the palindrome test

public class Palindrome {

    public static void main(String[] args) {
        palindrome();
    }

public static void palindrome(){
        //Create the a scanner object to collect user input However will be re-enabled at the end
        Scanner sc = new Scanner(System.in);

        //System.out.print();
        //Variable to store the user string
        String input = null;
        do {
       input = JOptionPane.showInputDialog(null,"Welcome to the Palindrome Test App.\n" + 
                                                    "Enter a string to test: ", "Palindrome App", 
                                                    JOptionPane.INFORMATION_MESSAGE);
        } while (input.isEmpty() == true);

        input = input.toLowerCase();// set the input to lower case 

        /****************************************THIS WAS ALL CLUTTER **********************************/
        /*input = input.replace(" ","").replace("!", "").replace(".", "").replace(",", "").replace("?","")
                .replace("-", "").replace("/", "").replace("\\","").replace(":", "")
                .replace("`", "").replace(";", "").replace("\"", "");*/
        //*************************************************************************************************/

        // Required a simple Regular expression to solve
        input=input.replaceAll("([[^\\w]])", "");
        System.out.println(input);

        //String dummy1 = "Jabroni"; // This is the first dummy palindrome string
        //dummy1 = dummy1.toLowerCase();
       // String dummy2 = ""; // This a non palindrome string

        int strLength = input.length(); // This will count the number of characters in a string
        int midCount = strLength / 2; // The "midCount" is divided into half so that we do not loop all the way

        // Initialize the character variables used to compare the chars in the loop
        char bChar; // Will store to check the beginning character
        char eChar; // Will store to check the ending char

        // Initialize a counter variable which will be used to compare
        //System.out.println(dummy1);

        int cCount = 0; // This will count how many chars are similar
        for (int x = 0; x < midCount; x++)
        {

            strLength--; // Reduces the string by 1
            //System.out.println(strLength);
            bChar = input.charAt(x);
            eChar = input.charAt(strLength);

            // If conditionals for comparison of characters
            if (bChar == eChar){
               cCount += 1;
            } 
        }

        String message = null;
        // Compare the midpoint with cCount 
        // If they match then the code is a success if not we have failed
        if (cCount == midCount){
            message = "Success! ;-D The string is a Palindrome";
        }
        else 
            message = "It is not a Palindrome. Sorry :-(";

        JOptionPane.showMessageDialog(null, message, "Palindrome App", JOptionPane.INFORMATION_MESSAGE);
    }     
}

That regex is almost right. It removes everything that's not a word character, but regex defines a "word character" as being made of any letters or numbers or underscores. (Presumably that was defined to be useful for parsing variables names ?) Your test data doesn't happen to have numbers or an underscore in it, so it doesn't reveal this bug.

If you just want to process letters in your palindrome test then the simplest correct regex would test explicitly just for letters, eg

s = s.toLowerCase().replaceAll("[^a-z]", ""); // delete everything that's not a-z
commented: I am keeping the numbers, just in case I ever come across a question similar to #4 in project Euler in the future. +3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.