0

I need help I have tried looking up many methods but can't seem to get it to work. I need this to not recognize white space, so if the user inputs le vel , it should say ,"Yes it is a Palindrome", just like if it was level //with no whitespace. The user input needs to end with a period and the program should not take into account the period. So level. should return true.

import java.util.Scanner;

public class PalindromeDemo
{
public static void main(String[] args)
{
    String phrase, answer;
    Scanner keyboard = new Scanner(System.in);
    do
    {
        System.out.println("I will determine if a string is a palindrome");
        System.out.println("Enter a word or characters and end it with a period");
        phrase = keyboard.nextLine();

        Palindrome pd = new Palindrome();
        if(pd.checkPalindrome(phrase))

                System.out.println("YES, the phrase is palindrome!");
            else
                System.out.println("NO, the phrase is NOT palindrome.");
                System.out.println();
                System.out.println("Would you like to continue? Enter yes or no");
                answer = keyboard.nextLine();
                System.out.println();
    }
    while(answer.equalsIgnoreCase("yes"));
}
}


public class Palindrome
{
public static final int MAX_CHARS = 80;

public boolean checkPalindrome(String text)
{
    char[] array = new char[80];
    int length = text.length();

    String reverseText = "";

    for(int i = length-1; i >= 0; i--)
    {
         reverseText = reverseText + text.charAt(i);
    }

    if(reverseText.equalsIgnoreCase(text))
    {
        return true;
    }

    else
    {
        return false;
    }
}
}
Mrs.Brightside
  • 115
  • 1
  • 1
  • 12
  • By the way, did you know that Eclipse (if you are using it) can format your code automatically? In Preferences, go to `Java > Code Style > Formatter` and enable it. You can also get recommendations using `Control+Space`. – ifly6 Feb 25 '16 at 07:46
  • I can only us TextPad for my class :/ – Mrs.Brightside Feb 25 '16 at 08:35

3 Answers3

1

Try this. Input string is not altered and this is faster and efficient than suggested above.

public static boolean checkPalindrome(String input){
    //input always contains period in the end.
    //ignore it.
    int length = input.length()-1-1;
    int i= 0;
    int j= length;
    while(i<j){
        if(input.charAt(i) == ' '){
            i++;
        } if (input.charAt(j) == ' '){
            j--;
        }
        if(input.charAt(i) ==input.charAt(j)){
            i++;
            j--;
        }else{
            return false;
        }
    }
    return true;

}
0

To remove the period, use .replace(".", ""). To remove leading and trailing whitespace, use .trim(). Both are applied on a String object. Very easy to just use pd.checkPalindrome(phrase.replace(".", "").trim()) where your if statement is right now.

Also,

if(pd.checkPalindrome(phrase))
    System.out.println("YES, the phrase is palindrome!");
else
    System.out.println("NO, the phrase is NOT palindrome.");
    System.out.println();
    System.out.println("Would you like to continue? Enter yes or no");
    answer = keyboard.nextLine();
    System.out.println();

Should have brackets to keep the logic in flow. Also, in Palindrome, why do you have a char[] which you don't use? Secondarily, I think the method checkPalindrome should be static and not require instantiation.

ifly6
  • 5,003
  • 2
  • 24
  • 47
  • Thanks for the feedback. I'm pretty new at this and we are barely learning about arrays. I didn't have it at first but looking at my instructions he said to use an array so I plugged it in but if there is no point I will take it out. Also could you give me an example for that method. I made it static and took out instantiation and got an error. – Mrs.Brightside Feb 25 '16 at 08:22
  • I just got it to work actually. I can see how it takes a step off – Mrs.Brightside Feb 25 '16 at 08:45
0

Use this method:

public boolean checkPalindrome(String text) {
    // remove all whitespace from input word (do this FIRST)
    text = text.replaceAll("\\s+", "");

    // remove optional period from end of input word
    if (text.endsWith(".")) {
        text = text.substring(0, text.length() - 1);
    }

    char[] array = new char[80];
    int length = text.length();

    String reverseText = "";

    for (int i = length-1; i >= 0; i--) {
         reverseText = reverseText + text.charAt(i);
    }

    if (reverseText.equalsIgnoreCase(text)) {
        return true;
    }
    else {
        return false;
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360