title: NumberFormatException For input String: "9646324351"
body: <p>Here's a snapshot of <a href="https://leetcode.com/problems/reverse-integer/" rel="nofollow noreferrer">a problem on LeetCode</a>:</p>
  
  <p><a href="https://i.stack.imgur.com/xxIRH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/xxIRH.png" alt="enter image description here"></a></p>
  
  <p>For this problem, they provide this framework code you have to fill in:</p>
  
  <pre><code>public int reverse(int x) {
  }
  </code></pre>
  
  <p>When testing solutions, it seems to provide an unreasonable input and won't let me submit my solution. The error is:</p>
  
  <p><a href="https://i.stack.imgur.com/Qcj3W.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qcj3W.png" alt="enter image description here"></a></p>
  
  <p>Note that the input is <code>1534236469</code>. That fits in an <code>int</code> as <em>input</em>, but the reversed version, <code>9646324351</code>, naturally doesn't, as the max positive value of an <code>int</code> in Java is <code>2147483647</code>.</p>
  
  <p>Is this just an error in the LeetCode test? Or is there some trick I can't imagine that magically lets me return <code>9646324351</code>, a clearly out-of-range value, back from <code>reverse</code> as an <code>int</code>?</p>
  
  <p>Here's my code, but the code almost doesn't matter, as the return type (<code>int</code>) is fixed by the problem (so by "integer" they really mean <code>int</code>, not <code>long</code>):</p>
  
  <pre><code>public int reverse(int x) {
      String intString;
      StringBuilder sb = new StringBuilder();
      if(x &lt; 0){
          sb.append("-");
          x = x * -1;
      }
      intString = Integer.toString(x);
      for(int i = intString.length() - 1; i &gt;= 0; i--){
          sb.append(intString.charAt(i));
      }
      String resultString = sb.toString();
      int result = Integer.parseInt(resultString);
      return result;
  }
  </code></pre>