BraxtonLanc
BraxtonLanc

Reputation: 11

Java program Reversing a number and determining if it is a Palindrome

as homework I am to write two methods; one that is a reversal method using public static int(int number) while the other is a Palindrome method using public static boolean isPalindrome(int number). I've been working on this for a couple hours and am honestly stumped. I'm not asking for my homework to be done for me, just help in understanding where to go from here. Thanks. My current code is as follows;

public class Exercise
{
    public static void main(String[] args)
    {

        System.out.println("Please enter an integer. ");
        Scanner input = new Scanner(System.in);
        int number = input.nextInt();
        boolean Final = isPalindrome(number);
        System.out.println(Final);

    }

    public static int reverse(int number)
    { // missing return?
        int y;
        int n;
        for (n = 0; n <= number; n++)
        { // parameters
            y = number % 10; // remainder
            number = number / 10; // gets rid of last digit
            n = n * 10 + y; // sets reverse values

            return n; // returns reversed number
        }
    }

    public static boolean isPalindrome(int number)
    {
        int n = reverse(number); // call reverse method
        boolean result; // declare result
        if (n = number)
        { // incompatible types?
            result = true;
            System.out.println("The number " + number + " is a " + "Palindrome" + ".");
        }
        else if (n != number)
        {
            result = false;
            System.out.println("The number " + number + " is a Palindrome" + ".");
        }
        return result; // not initialized?
    }
}

Upvotes: 1

Views: 16520

Answers (6)

huseyin
huseyin

Reputation: 1427

Simply get the digit count of the number via Math functions and then iterate by using '/' and '%' operations as follows. After x = (x % divider) / 10, we should divide divider by 100 since we made 2 zero operations.

public static boolean isPalindrome(int x) {
        if (x < 0) return false;
        if (x < 10) return true;

        int numDigits = (int)(Math.log10(x)+1);
        int divider = (int) (Math.pow(10, numDigits - 1));
        for (int i = 0; i < numDigits / 2; i++) {
            if (x / divider != x % 10)
                return false;
            x = (x % divider) / 10;
            divider /= 100;
        }
        return true;
    }

Upvotes: 0

Sree
Sree

Reputation: 1

If you are writing for Number palindrome. here's the tip:

boolean method

public static boolean ispalindrome(int number)
    {
        int num=reverse(number);
        boolean res;
        if (num == number){
            res=true;
        System.out.println("The Entered number "+number+ " is a palindrome");}
        else {
            res=false;
            System.out.println("The Entered number "+number+" is not a palindrome");}
        return res;
    }

in your main method ,

boolean Res=ispallindrome(number);  

can be used to check the condition.

Upvotes: 0

Yatin Dabhat
Yatin Dabhat

Reputation: 11

/* Palindrome program using methods and Interface */

 import java.util.Scanner;

 interface Test    //  All methods inside interface are treated "Public"
    {                 //  and abstract in nature by default   
String getInput();
boolean IsPalindrome(String s);
void displayInput();
         }

  abstract class Sub_Test  implements Test
       {
   public String getInput()
   {
       Scanner scan = new Scanner(System.in);
       System.out.println("Enter a String ");
       String str = scan.nextLine();
       return str;
   }
   public boolean IsPalindrome(String s)
       { 
       int limit = s.length() - 1;
       char st1 [] = s.toCharArray();
   Inner:for( int i = 0,j = limit; i < j ; i++,j--)
       {
           if(st1[i] == st1[j])
               return true;
           else 
               break Inner;
       }
              return false;
   }
          public void displayInput()
            {
      String input = getInput();
      boolean word = IsPalindrome(input);
      if(word)
        System.out.println("Given String "+input+" is palindrome ");
      else
        System.out.println("Given String "+input+" is NOT palindrome ");
                }   // End of displayInput() method

             }  // End of Sub_test abstract class

    public class String_Session1 extends Sub_Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    String_Session1 object = new String_Session1();
    object.displayInput();
}
          }  // End of String-Session1 Main class

/* The output is as below */

   Enter a String 
   Race CAR

   Given String Race CAR is palindrome

Upvotes: 1

Jiman
Jiman

Reputation: 185

It's easier to work with strings for palindromes IMO.

Souce post: How do you determine if a String is a palindrome?

int someNumber = 12321;
String palindrome = someNumber+"";
boolean isPalindrome = palindrome.equals(new StringBuilder(palindrome).reverse().toString());

With your code, it would be:

public static void main(String[] args)
{

    System.out.println("Please enter an integer. ");
    Scanner input = new Scanner(System.in);
    int number = input.nextInt();
    String palindrome = number+"";
    boolean result = palindrome.equals(new StringBuilder(palindrome).reverse().toString());
    System.out.println(result);

}

Upvotes: 1

durron597
durron597

Reputation: 32323

Your return n; is inside the for loop. Just move the curly brace to above it.

Also, change the for loop to while (number > 0) {

And change (n = number) to (n == number)

Also, delete the second if condition in isPalindrome, you don't need it. Just put else. That will make the compiler tell you that return result; is unreachable... just delete it.

Upvotes: 3

kjana83
kjana83

Reputation: 398

Problem in reverse logic.

try this

 public static int reverse(int number)
        {  
            int result = 0;
            int remainder;
            while (number > 0)
            {
                remainder = number % 10;
                number = number / 10;
                result = result * 10 + remainder;
            }

            return result;
        }

Upvotes: 1

Related Questions