Question:
Java Coding Help (Palindromes)?
Spellmaster DuskShadow
2013-06-08 12:05:03 UTC
Task: Write a program that gets input from a user and tests if it is a palindrome. Use methods.

Here's what I have:

import java.util.Scanner;

public class PalindromeInteger {
public static int reverse(int number) {
int temp = 0;
temp = temp * 10;
temp = temp + number % 10;
number = number / 10;
return temp;

}

public static boolean isPalindrome(int number) {
int reversed = reverse(number);
if (reversed != number)
return false;
else
return true;

}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a int ");
int number = input.nextInt();
boolean Palindrome = isPalindrome(number);
if (Palindrome = true)
System.out.println("Is a palindrome");
else
System.out.println("Is not a palindrome");
}
}


It ALWAYS says it is not a palindrome. What am I doing wrong? Thanks

Feel free to be rude or condescending, as long as you actually help. I don't want you to code it, i want to know what I'm doing wrong.
Three answers:
2013-06-08 12:20:06 UTC
The reverse function is wrong, ultimately it will always return the following and nothing more:



number % 10



I recommend taking a piece of paper and making two columns, one with temp and one with number. Now give number a random value and move line by line in your reverse function and write down how each variable changes. This can help you see where you went wrong. For example, I'll go through your code and add comments to show whats happening



// let's say number is 12345

public static int reverse(int number) {



// ok temp is 0

int temp = 0;



// temp is still 0 because 0 * 10 = 0

temp = temp * 10;



// remember, that temp =0 so now we are doing 0 + 12345 % 10

// this is 5, so right now temp is 5

temp = temp + number % 10;



// now we are taking number, which has not changed from 12345 and diving it by 10

// now number is 1234 (with int's you drop the decimal)

number = number / 10;



// ok at this point number is 1234 and temp is 5



// we return 5

return temp;



}





so with this function you can see that reverse(12345) would return 5, this is clearly not what we want. I'm not sure if this is for homework, but if there's no rules saying you can't convert values I would recommend looking up how to convert a number to a string, Java has functions built in to reverse a string, it may be easier to check for a palindrome this way. The logic would be



if string(number) == string(number).reverse then palindrome





EDIT: hmm, srry I haven't used Java in a long time, it looks like Java does not have a striaghtforward 'reverse', its a little more involved:



http://stackoverflow.com/questions/2441501/reverse-hello-world-in-java





also:

http://stackoverflow.com/questions/4105331/how-to-convert-from-int-to-string
Bob
2013-06-08 19:31:06 UTC
The reverse method is not correct. Consider inserting a loop into your method to get the reversed number. Also, you are assigning the boolean variable 'Palindrome' the value 'true' so it will always print out "Is a palindrome".



Use the '==' operator to compare Palindrome with true:



if(Palindrome == true)
Zoltie
2013-06-08 20:25:12 UTC
The reverse method is wrong. Remember, you are dealing with itegers, not strings. You can't concatenate numbers the way you can strings. The best way to do this would be to make the "temp" variable a string.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...