-1

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.

Julie
  • 6,221
  • 3
  • 31
  • 37
AllWillB1
  • 13
  • 3

5 Answers5

0

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--)
Peter Yao
  • 101
  • 4
  • No this is the java i produced after the assignment to try and get some practice its just bugging me i cant get it to work. i removed the semicolon but it still comes back labeling "212" as not a palindrome. so i think i made some error that remains hidden to me. – AllWillB1 Jun 11 '15 at 03:50
0

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...");
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
0

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!

Vihar
  • 3,626
  • 2
  • 24
  • 47
  • Thank you this correction made it work for values like "212" or "rotor" however spaces are still throwing it off so "212 212" or "race car" still come back false. is there i way i can correct for that. our would a StringBuilder or array approach as listed by you and others be required to provide that functionality? – AllWillB1 Jun 11 '15 at 04:02
  • "race car" is not a palindrome, you need to respect the space, and generally palindrome is considered for single words, if you still want to do it, we can think of something like removing space and then checking – Vihar Jun 11 '15 at 04:05
  • Thank you very much. I will consider this a success then. I suppose i could ask them to enter phrases with out spaces if they desire a phrase rather then just a word or number. I will look into the function of StringBuilder it sounds like that can save me some head aches in the future. – AllWillB1 Jun 11 '15 at 04:08
0

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.

Ouney
  • 1,164
  • 1
  • 10
  • 22
  • thank you i will google how to use the array system I have not used that before, new to coding. it seems the more mathematically rigorous way of doing it at first glance. – AllWillB1 Jun 11 '15 at 04:04
  • Sure...it is like "we have two pointers - one from start and another from end and we are comparing the characters they point to and then progress in the same manner unless we get a mismatch (in which case it will stop immediately) OR both pointers meet in center which means it is a palindrome.So, if given word is 'Ouney' then it will compare 'O' and 'y' and will fail (only one iteration) but in your version for each word it will loop the same number of times. – Ouney Jun 11 '15 at 04:11
0

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));
    }
}
Peter Pan
  • 23,476
  • 4
  • 25
  • 43