0

I dont know why it won't work... I've tried changing it from int to long... The last input 46894 causes a problem... Help me please.
Here is my code:

import java.util.*;

public class palimdrone
{
   public static void main(String[] args)
   {
      Scanner scan = new Scanner(System.in);
      int[] number = new int[12];

      int input = scan.nextInt();

      for(int x=0;x<input;x++)
      {  
         number[x] = scan.nextInt();
      }

      for(int a=0;a<input;a++)
      {
         long tot =0,sumA=0,sumB=0,attempt=0;
         sumA = number[a];sumB=reverse(number[a]);
         boolean palin=false;

         if(sumA==sumB)
         {
            palin = true;
            attempt++;
         }
         else
         {
            while(attempt!=10)
            {
               attempt++;               
               tot = sumA+sumB;
               if(tot == reverse(tot))
               {
                  palin=true;  
                  break;
               }

               sumA=tot;
               sumB=reverse(tot);    
            }
         }

         if(palin==true)
            System.out.println(tot+" is Palindrome ; Attempt: "+attempt);
         else
            System.out.println(tot+"; None");
      }
   }

   public static long reverse(long num)
   {
      String tnum=""+num;
      String reverse="";
      for(int x=tnum.length()-1;x>=0;x--)
      {
         reverse = reverse+tnum.charAt(x);
      }

      num = Integer.parseInt(reverse);
      return num;
   }

}

Here's the input

87<br>
196<br>
1689<br>
46785<br>
46894 <-- Error Here<br><br>

Here's the output

4884 is Palindrome ; Attempt: 4<br>
18211171; None<br>
56265 is Palindrome ; Attempt: 4<br>
1552551 is Palindrome ; Attempt: 3<br>
Exception in thread "main" java.lang.NumberFormatException: For input string: "2284457131"<br>
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)<br>
at java.lang.Integer.parseInt(Integer.java:583)<br>
at java.lang.Integer.parseInt(Integer.java:615)<br>
at palimdrone.reverse(palimdrone.java:61)<br>
at palimdrone.main(palimdrone.java:34)<br>
Aleksander Lidtke
  • 2,876
  • 4
  • 29
  • 41
ZeroOne
  • 8,996
  • 4
  • 27
  • 45
  • 1
    Take a look at the maximal value that can be held inside an `int`: http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html – Jeroen Vannevel Sep 06 '14 at 10:50
  • 1
    BTW: reversing a string is already implemented in java: [StringBuilder.reverse()](http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html#reverse%28%29). See also [SO question: "Reverse a string in Java"](http://stackoverflow.com/q/7569335/2991525) – fabian Sep 06 '14 at 11:00

2 Answers2

4

2284457131 is greater than Integer.MAX_VALUE. Try using Long.parseLong(reverse) instead of Integer.parseInt(reverse).

ortis
  • 2,203
  • 2
  • 15
  • 18
0

It's better to parse the input as String instead of number. So you will never have to worry about overflow.

static boolean isPalindrome(String str) {
    StringBuilder strBuilder = new StringBuilder(str);
    strBuilder = strBuilder.reverse();

    return str.equals(strBuilder.toString());
}

take input as String or convert the integer to string

    Scanner scan = new Scanner(System.in);
    String input = scan.next();
    System.out.println(isPalindrome(input));
mirmdasif
  • 6,014
  • 2
  • 22
  • 28