I am just plain stuck. I can not figue out why when I type a word to test a palindrome sometimes it comes back as true and other times as false. Thank you for any help.

import java.util.*;

public class ec1
{
    static Scanner kb = new Scanner(System.in);

    public static void main(String[] args)
    {
        String word = " ";
        String s = " ";
        char letter;
        int i=0, j=0;
        boolean isPal;

        System.out.println("Enter a sentence to test for a palindrome (CTRL-Z to end): ");
        while (kb.hasNext() )
        {
             s =kb.nextLine();
             for (i=0; i < s.length(); i++) {
             letter = s.charAt(i);
             if(Character.isLetter(letter))
             {
                 word += letter;
             }

        }
            if(isPal(s))
            System.out.println("That is a Palindrome");
            else
            System.out.println("That is not a Palindrome");
            word = "";
            System.out.println("Enter a sentence to test for palindrome (CTRL-Z to end): ");
            }
    }
    public static boolean isPal(String s)
    {
        int len = s.length();
        int i= 0, j = 0;
        char ch1, ch;
        j = len - 1;
        boolean found = false;
        for (i = 0; i < len; i++)
        {
            ch = s.charAt(i);
            ch1 = s.charAt(j);
            if(!Character.isLetter(ch) || !Character.isLetter(ch1) || ch != ch1){
                j--;
                return false;
                }else{
            }
        }       return true;

    }
}

Recommended Answers

All 4 Replies

Please use code tags and indent your code correctly it is a nightmare to read
And why check for isLetter ?
just if charAt(i) == charAt(j) should do

Member Avatar for coil

There are a few issues with your code.

1. You state "CTRL-Z" to end, but nowhere do you actually check whether that key combination has been pressed (this would actually not be that easy to implement, so I would just take it out altogether).

2. So far as I can see, you're using a for-loop to modify s and create a new String word with only the letters (presumably to remove spaces), but you don't use the variable word - that means you either mistakenly coded the program to check the wrong String or you're forcing the computer to do extra work for no reason. By the way, if you are trying to replace all the spaces, the String class has a built-in replaceAll method just for that.

3. I think you need to clean up your isPal method.

As pbl said, you don't need to check whether the character is a letter - you already did that in your for-loop with the variable word when you took in input. Also, why decrement j , just before breaking out of the loop and returning?

By the way, I think the source of your problem is that you are not decrementing j if the characters match.

e.g. isPal("adcda")

Loop 1 i=0 j=4 'a'='a'
Loop 2 i=1 j=4 'd'≠'a'

You should be able to figure out how to fix your program from there.

Please use code tags and indent your code correctly it is a nightmare to read
And why check for isLetter ?
just if charAt(i) == charAt(j) should do

Ok Thanks pbl will do.

There are a few issues with your code.

1. You state "CTRL-Z" to end, but nowhere do you actually check whether that key combination has been pressed (this would actually not be that easy to implement, so I would just take it out altogether).

2. So far as I can see, you're using a for-loop to modify s and create a new String word with only the letters (presumably to remove spaces), but you don't use the variable word - that means you either mistakenly coded the program to check the wrong String or you're forcing the computer to do extra work for no reason. By the way, if you are trying to replace all the spaces, the String class has a built-in replaceAll method just for that.

3. I think you need to clean up your isPal method.

As pbl said, you don't need to check whether the character is a letter - you already did that in your for-loop with the variable word when you took in input. Also, why decrement j , just before breaking out of the loop and returning?

By the way, I think the source of your problem is that you are not decrementing j if the characters match.

e.g. isPal("adcda")

Loop 1 i=0 j=4 'a'='a'
Loop 2 i=1 j=4 'd'≠'a'

You should be able to figure out how to fix your program from there.

Thanks for your help Coil. I will continue to work on this. It was for a school project but now I just want to figure it out anyway.

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.