JAVA: Error reversing a string to check for palaindrome off-by-one i think

470 views Asked by At

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.

5

There are 5 answers

3
Vihar On BEST ANSWER

the for loop should be something like this

    for (reverseCountdown = stringLength-1; reverseCountdown >=0; reverseCountdown--){ //have changed the loop variables here
                reverse  += forward.charAt(reverseCountdown);
    }

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!

1
Peter Yao On

This is not pseudocode right? Why do you have a semicolon after your for loop?

I would remove that.

That's not the only issue. You should be using your reverseCountdown as the loop check not stringLength and you should be decrementing the reverseCountdown.

for(reverseCountdown = stringLength-1; reverseCountdown >= 0; reverseCountdown--)
2
Elliott Frisch On

Your for loop is terminated by the semicolon.

for(reverseCountdown = stringLength-1; stringLength>-1; stringLength--);
reverse=reverse+forward.charAt(reverseCountdown);

I believe you wanted (and you need to test and modify reverseCountdown) something like

for(reverseCountdown = stringLength-1; reverseCountdown>-1; reverseCountdown--) {
   reverse=reverse+forward.charAt(reverseCountdown);
}

I would personally prefer StringBuilder (because it has a reverse method) like

System.out.println("enter a string and I will tell you if "
        + "it's a palindrome");
String forward = in.next();
StringBuilder sb = new StringBuilder(forward);
sb.reverse();
if (sb.toString().equals(forward)) {
    System.out.println("Bro you got a palindrome!");
} else {
    System.out.println("Thats not a palindrome...");
}
2
Ouney On

I think a better approach would either to use StringBuilder in java or make your own implementation using array like below.

import java.util.Scanner;


public class PalindromeCheck {

private static Scanner in; 

public static void main(String[] args) {

    in = new Scanner(System.in);
    System.out.println("enter a string and I will tell you if its a palindrome");
    char[] arr = in.next().toCharArray();
    boolean bool = true;
    for(int i=0;i<Math.floor(arr.length/2);i++){
        if(arr[i] != arr[arr.length-1-i]){
            System.out.println("Not a palindrome");
            bool = false;
            break;
        }
    }
    if(bool){
        System.out.println("You got a palindrome");
    }
}
}

This would be more efficient as it loops only half of the times.

0
Peter Pan On

Check Palindrome Code

public class PalindromeCheck {

    public static boolean check(String str) {
        boolean flag = true;
        char[] chs = str.toCharArray();
        int len = chs.length;
        for(int i = 0; i < len/2; i++) {
            if(chs[i] != chs[len - i -1]) {
                flag = false;
                break;
            }
        }
        return flag;
    }

    public static void main(String[] args) {
        String str0 = "123321";
        String str1 = "1234321";
        String str2 = "123421";
        System.out.println(check(str0));
        System.out.println(check(str1));
        System.out.println(check(str2));
    }
}