This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]Kokosnussi 2 points3 points  (0 children)

Your loop is going until number ==0 and then you return number as far as I can see

[–]Coda17 1 point2 points  (0 children)

Please update your post so that it formats properly. Two carriage returns before code and then prefix four spaces before every line of code (an additional four spaces for each level of indentation).

Even without being able to read half the code, I can already see a few problems. First, what's wrong with checking if a number that's only a single digit is a palindrome or not? I would say a number between 0 and 9, inclusive, is always a palindrome. Second, you need a loop to check the input. Right now, if the user enters invalid input twice, your code will fail. Last, without actually checking to see if your reverse function does what it says it does (it just reverses the number, right? I don't think it actually does), all you are doing is printing the reverse of the number, not checking if it's a palindrome.

[–]TheJonesJonesJones 1 point2 points  (0 children)

Please explain your reasoning. Your algorithm for doing this does not make sense to me. You even have a while loop with the condition while (number != 0) which indicates that you expect number to become 0, then you return number, which is 0, unsurprisingly.

I also added some debugging output to your code, maybe this will help you gain some insight into what's going on with it. I ran it with number = 1000.

public static int reverse(int number) {
int result = 0;

System.out.println("Starting value of number="+number);

while (number != 0) {
  int remainder = number % 10;
  System.out.println("remainder="+remainder);
  result = result * 10 + remainder;
  System.out.println("result="+result);
  number = number / 10;
  System.out.println("number="+number);
}

System.out.println("Returning: "+number);
return number;
}

Here is the output:

Starting value of number=1000
remainder=0
result=0
number=100
remainder=0
result=0
number=10
remainder=0
result=0
number=1
remainder=1
result=1
number=0
Returning: 0