1

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?
    }
}
Vikdor
  • 23,934
  • 10
  • 61
  • 84
BraxtonLanc
  • 11
  • 1
  • 2
  • 5
  • Your code, as posted, has indentation that does not match the logic. That can make it harder to spot problems. If it is that way in your working copy, the first thing I would do is load it into an IDE and reformat. – Patricia Shanahan Nov 05 '12 at 04:46
  • 2
    Does it even compile? (n = number) is not a valid boolean expression. – Vikdor Nov 05 '12 at 04:48
  • Sorry, as posted is not how it appears in NetBeans. No it did not compile. I've been stuck on all the little dumb errors I tend to make, then I will actually be able to tell if I've incorporated the methods together in the main class properly. Thanks, Braxton – BraxtonLanc Nov 05 '12 at 20:22

6 Answers6

3

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.

durron597
  • 31,968
  • 17
  • 99
  • 158
1

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;
        }
kjana83
  • 398
  • 3
  • 16
1

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);

}
Community
  • 1
  • 1
Jiman
  • 185
  • 2
  • 13
1

/* 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
0

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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Sree
  • 1
0

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;
    }
huseyin
  • 1,367
  • 16
  • 19