Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Any help please!

843810Apr 8 2004 — edited Apr 8 2004
I am creating a program which reads a string of 5 digits and then outputs whether it is a palindrome or not. I have completed it but need to add an extra bit to it, I am trying to add a do while statement, so if the string is not 5 digits long, the string must be inputted again. Can someone tell me where my do while statement has gone wrong?

Any help greatfully recevied, thank you




public class Palindrome2 {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) {
do

{System.out.println("Please enter a 5 digit integer");
String string;
string=EasyIn.getString();
if (string.length()==5) {

System.out.println("Thank you the program is now testing to see if your integer is a palindrome");
} else {

System.out.println("Please enter an integer which is 5 digits long");
}
}while (string.length()!=5);

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();
}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 6 2004
Added on Apr 8 2004
1 comment
82 views