I am having a problem. I received an assignment to write pseudo code for a palindrome checking program. My problem is that while I received good marks on my pseudocode assignment, when I tried to write the code in java for my own edification, I was unable to make one capable of checking int
and string
.
import java.util.Scanner;
public class palindromeCheck {
private static Scanner in;
public static void main(String[] args) {
in = new Scanner(System.in);
String forward;
String reverse = "";
int reverseCountdown;
System.out.println("enter a string and I will tell you if its a palindrome");
forward= in.next();
int stringLength= forward.length();
for(reverseCountdown = stringLength-1; stringLength>-1; stringLength--);
reverse=reverse+forward.charAt(reverseCountdown);
if(forward.equals(reverse))
System.out.println("Bro you got a palindrome!");
else
System.out.println("Thats not a palindrome...");
}
}
Now my problem as far as I can find it with my pitiful skills, is that in my for
loop, I am transcribing character values over to a string one by one, however, I can not come up with a code solution that will take all the characters; it seems to take them all but one. (or perhaps my error is something else.) but that is what it looks like to me as the code will run, but I never get a response of a palindrome (even for something obvious like 222), other than with single character items like 0 or 1.
Any help fixing this or even understanding a more elegant way to check would be appreciated.
the for loop should be something like this
the
;
at the end of for loop was causing the for loop to run on empty statement(only ; is considered as empty statement). remove that and changed your for loop a bit,problem with your for-loop was , you should have used the variable 'reverseCountdown' everywhere, but you were doing
StringLength--
which wasnt going great.Using StringBuilder is also a way,it has in-built function to reverse a string, but I am not sure if the person who gave you assignment would be happy to see you using the in-built function .
hope this helps! Good luck!