title: NumberFormatException when parsing negative integer
body: <p>I am writing a java program to determine if a number is a palindrome.</p>
  
  <p>My code works properly if the argument passed is a positive integer, but throws a NumberFormatException when passing a negative integer.</p>
  
  <pre><code>Exception in thread "main" java.lang.NumberFormatException: For input string: ""
      at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
  at java.base/java.lang.Integer.parseInt(Integer.java:662)
  at java.base/java.lang.Integer.parseInt(Integer.java:770)
  at com.stu.Main.isPalindrome(Main.java:28)
  at com.stu.Main.main(Main.java:7)
  </code></pre>
  
  <p>The following solution I took from another stackoverflow thread, which seems to be what the instructor wants us to use, however in the while loop I assume that since a negative number is always less than 0, it will break out of the loop and not calculate the palindrome:</p>
  
  <pre><code>public static int reverse(int number)
          {  
              int result = 0;
              int remainder;
              while (number &gt; 0)
              {
                  remainder = number % 10;
                  number = number / 10;
                  result = result * 10 + remainder;
              }
  
              return result;
          }
  </code></pre>
  
  <p>So, I am using strings in my solution below to solve this issue.</p>
  
  <p>Note: we have not yet got to splits and regexps yet.</p>
  
  <pre><code>public class Main {
  
      public static void main(String[] args) {
  
          isPalindrome(-1221); // throws exception
          isPalindrome(707);   // works as expected - returns true
          isPalindrome(11212); // works as expected - returns false
          isPalindrome(123321);// works as expected - returns true
      }
  
  
      public static boolean isPalindrome(int number){
  
          if(number &lt; 10 &amp;&amp; number &gt; -10) {
              return false;
          }
  
          String origNumber = String.valueOf(number);
          String reverse = "";
  
          while(number &gt; 0) {
              reverse += String.valueOf(number % 10);
              number /= 10;
          }
  
          if(Integer.parseInt(origNumber) == Integer.parseInt(reverse)) {
              System.out.println("The original number was " + origNumber + "     and the reverse is " + reverse);
              System.out.println("Number is a palindrome!");
              return true;
          }
          else
              System.out.println("The original number was " + origNumber + " and the reverse is " + reverse);
              System.out.println("Sorry, the number is NOT a palindrome!");
              return false;
      }
  }
  </code></pre>
  
  <p>I am looking for two things here. </p>
  
  <p>First, how do I solve the issue with the negative number in the while loop in the case of the instructors preferred solution?</p>
  
  <p>Second, how can I solve the NumberFormatException in my solution?</p>
  
  <p>Edit: a third question. Why does my solution return false if I never parse back to an int?</p>
  
  <pre><code>if(Integer.parseInt(origNumber) == Integer.parseInt(reverse)) // works
  </code></pre>
  
  <p>to</p>
  
  <pre><code>if(origNumber == reverse) // does not work
  </code></pre>
  
  <p>Thanks!</p>