Author: Jason Kent
Date: 15:59:05 06/01/04
Go up one level in this thread
I'm taking a java course in college. "The Java Tutorial, Third Edition A Short
Course on the Basics". Overall I think java is pretty neat. How fast is it
compared with other languages for chess engines?
I'm almost done with this book but I'm still struggling with a "simple app"!
I want to create a simple program that can take any 3 digit number and make it a
palindromic number.
By hand you would do this by:
1. Writing down any 3 digit number
2. Reversing the current number and adding it to the current number
3. Continue step 2 until its a palindrome.
I've already got a source file that can detect whether I've got a palindrome in
string format.
Any help would be appreciated.
public class PalindromeNumb{
public static boolean isPalindrome(String stringToTest) {
String workingCopy = removeJunk(stringToTest);
String reversedCopy = reverse(workingCopy);
return reversedCopy.equalsIgnoreCase(workingCopy);
}
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();
}
protected static String reverse(String string) {
StringBuffer sb = new StringBuffer(string);
return sb.reverse().toString();
}
public static void main(String[] args) {
String string = "121";
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();
}
}
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.