Click to See Complete Forum and Search --> : Need Help with Palindrome Program


sparkyg5
03-06-2007, 03:14 AM
I am having trouble with this palindrome program. I am trying to write a program that generates palindromes from 100-300

For each number between first and last, my program has to do the following:


1. Convert the number to a string.
2. If the string is a palindrome:
3. Display an adequate message.
4. Find the square of the original number that turned out to be a palindrome.
5. Convert the square to yet another string
6. Test if this second string is a palindrome, too, and if so, display
an adequate message.
7. If the string is not a palindrome, do not display anything.


Here is the code:
/**
* This class tests if a given string is a palindrome.
* Palindromes are words, numbers, and phrases
* that can be read the same backwards as forwards.
*/
public class Palindrome {

/**
* Tests if its string parameter is a palindrome.
*/


public static boolean isPalindrome(String stringToTest) {



String workingCopy = removeJunk(stringToTest);
String reversedCopy = reverse(workingCopy);
return reversedCopy.equalsIgnoreCase(workingCopy);



}


/**
* Returns a reversed copy of a string with all non-alphanumeric
characters stripped.
*/


protected static String removeJunk(String string) {



int i, len = string.length();
StringBuffer dest = new StringBuffer(len);
char c;

for (i = (len - 1); i >= 0; i--) {



c = string.charAt(i);
if (Character.isLetterOrDigit(c)) {
dest.append(c);
}



}
return dest.toString();



}


/**
* Returns a reversed copy of a string.
*/

protected static String reverse(String string) {



StringBuffer sb = new StringBuffer(string);
return sb.reverse().toString();



}


/**
* Provides an illustration of how to test if a string is a palindrome.
*/


public static void main(String[] args) {



String string = "Madam, I'm Adam.";
System.out.println();
System.out.println("Testing whether the following "
+ "string is a palindrome:");
System.out.println(" " + string);
System.out.println();

if (isPalindrome(string)) {


System.out.println("It IS a palindrome!");



} else {



System.out.println("It is NOT a palindrome!");



}
System.out.println();



}

}

Any help or hints would be greatly appreciated!!!

agent_x91
03-06-2007, 05:56 AM
My friend was doing the exact same assignment as you. What exactly are you having difficulty with? You've got a method to test whether it is a palindrome or not but you don't seem to have made any attempt to complete it yourself -.-

sparkyg5
03-06-2007, 02:57 PM
One of the problems I am having is converting the number into a string. How to start??? Just need a push in the right direction thats all!!

jasonahoule
03-06-2007, 05:31 PM
Converting a number to a string is simple:

int myInt = 101;
String myString = "" + myInt;

myString is now equal to 101 and is a string.

agent_x91
03-10-2007, 11:45 AM
As jasonahoule says, adding an empty String to an int will turn it into a String. You should then be able to use loops and string mainpulation methods to check if the String reads the same backwards and forwards (and therefore whether or not it is a palindrome).