I have been able to output a palindrome but now it does so even if it is not a palindrome. Thanks you in advance for any help. ;)

import java.util.*;

public class ec2
{
	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.print("Enter a palindrome (CTRL-D 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(word))
			System.out.println(word + " is a Palindrome");
			else
			System.out.println(word + " is not a Palindrome");
			word = "";
			System.out.println("Enter a palindrome (CTRL-D to end): ");
			}
	}
    public static boolean isPal(String s)
	{
		int len = s.length();
		int i, j;
		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)
			{
				found = false;
				}
				else
				{
			    found = true;
				}
				j--;
				}
			    return found;

	}
}

Recommended Answers

All 6 Replies

Seems your problem lies in your logic for testing palindromes.

if(!Character.isLetter(ch) && !Character.isLetter(ch1) && ch != ch1)
			{
				found = false;
				}

So, this means, your method will only return false if all these expressions are true.

So, if I enter "hole": the first two checks will return false(they are letters),but the last will return true, therefore it will never enter that block. Using || instead should fix your problem.

There is still a gaping hole in the logic for spotting a palindrome, for instance, I can type a words like evaluate,designated and these will all be palindromes according to your program!

Seems your problem lies in your logic for testing palindromes.

if(!Character.isLetter(ch) && !Character.isLetter(ch1) && ch != ch1)
			{
				found = false;
				}

So, this means, your method will only return false if all these expressions are true.

So, if I enter "hole": the first two checks will return false(they are letters),but the last will return true, therefore it will never enter that block. Using || instead should fix your problem.

There is still a gaping hole in the logic for spotting a palindrome, for instance, I can type a words like evaluate,designated and these will all be palindromes according to your program!

Thanks for your help Akill10. Is this what you mean?

if(!Character.isLetter(ch) &&!Character.isLetter(ch1) || ch != ch1)
			{
				j--;
				found = false;
				}
				else
				{
			    found = true;
				}
				}
				return found;
	}
}

Thanks for your help Akill10. Is this what you mean?

if(!Character.isLetter(ch) &&!Character.isLetter(ch1) || ch != ch1)
			{
				j--;
				found = false;
				}
				else
				{
			    found = true;
				}
				}
				return found;
	}
}

Yes, but don't you think it would be even better if you made them all || ? Think about it, you don't want anything that starts OR ends with something that is not a letter.

So, altogether it goes: if(1st char is not a letter or 2nd is not a letter orthe first and last do not match)...if ANY of these are true, then it is NOT a palindrome.

Yes, but don't you think it would be even better if you made them all || ? Think about it, you don't want anything that starts OR ends with something that is not a letter.

So, altogether it goes: if(1st char is not a letter or 2nd is not a letter orthe first and last do not match)...if ANY of these are true, then it is NOT a palindrome.

That makes a lot of sense. Thanks again Akill10. By the way that is cool name. :)

Ha, no problem. It's nearly as good as my actual second name 'killingbeck', not even joking! Don't forget to mark as solved!

ya there is logical problem in that IF condition u check out That Condition And Then Try out surely it you will get the out put

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.