-1

My Java code is here:

import java.util.Scanner;
public class task2 {
    public static void main(String args[])  {
        System.out.print("Input a 3 digit int");

        Scanner scan = new Scanner(System.in);
        int x = scan.nextInt();

        int isPalindrome = 0; 

        while (x != 0)
        {
            isPalindrome = isPalindrome*10 + x % 10;
            x /= 10;
        }

        {
            if (x == isPalindrome){
                System.out.print ("Yes, this is a palindrome!");
            }
            else {
                System.out.print("No, try again");
            }
        }
    }
}

The code will only recognize a palindrome if the numbers entered are zeroes. I'm having trouble understanding why.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Answer here: http://stackoverflow.com/questions/199184/how-do-i-check-if-a-number-is-a-palindrome – nem035 Sep 10 '14 at 23:50
  • I have to applaud your choice of tags. While looking for basic Java advice, you found java, BASIC (language), and stumbled into aspect-oriented programming on the way there. – nanofarad Sep 10 '14 at 23:52

5 Answers5

1

This is because the value of x is getting changed finally.Which is not the original number at the end of the program. SO take another variable just below x like: int y = x; And at the end while using "if" condition use this value of y for comparison rather than using x. It will run perfectly.

int x = scan.nextInt();

int y=x;

if (y == isPalindrome) Add new variable like this.

Narendra Jadon
  • 148
  • 1
  • 11
0

The problem with you solution is that you modify x in the while loop. Your final check of (x == isPalindrome) will always fail because you will only reach that statement when x is equal to zero.

You need to save the original x value in another variable and use that to check against isPalindrome.

Jesse J
  • 532
  • 1
  • 7
  • 18
0

The problem is that in the course of processing the value of x is being changed from what was originally input - it always ends up as 0.

So, you have to preserve the input value, like so:

Scanner scan = new Scanner(System.in);
int original = scan.nextInt();
int x = original;

and then use the original value for the final comparison, like so:

if (original == isPalindrome){
Russ Jackson
  • 1,993
  • 1
  • 18
  • 14
0

Here's how I'd do it: I'd use the libraries more and write a lot less code.

I'd recommend that you learn the Sun Java coding standards and develop a formatting style. Readability promotes understanding. Style and neatness matter.

package misc;

public class PalindromeChecker {
    public static void main(String args[]) {
        for (String arg : args) {
            System.out.println(String.format("arg '%s' is" + (isPalindrome(Integer.valueOf(arg)) ? "" : " not") + " a palindrome", arg));
        }
    }

    public static boolean isPalindrome(int value) {
        String s = Integer.toString(value);
        String reversed = new StringBuilder(s).reverse().toString();
        return reversed.equals(s);
    }
}
duffymo
  • 305,152
  • 44
  • 369
  • 561
0

When you do the following operation on x:

x /= 10;

you're modifying its value - so it no longer contains the input from:

int x = scan.nextInt();

As Narendra Jadon suggested - you can save the original value into another variable and use it when you try to compare:

if (x == isPalindrome){

Alternative solution that uses "conversion" of the int to String:

public boolean isPalindrom(int n) {
    return new StringBuilder("" + n).reverse().toString().equals("" + n);
}
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129