title: Java ParseInt() - Catching Strings with a leading zero
body: <p>Java's <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int)" rel="nofollow">ParseInt</a> method will happily parse decimal values supplied with a leading zero without throwing an exception, stripping the zero:</p>
  
  <pre><code>int value = Integer.parseInt("050", 10);
  </code></pre>
  
  <p>will result in the integer value 50. </p>
  
  <p>But, I have an application requiring a string such as this to be rejected as invalid input. My solution to the problem so far has been to convert the parsed integer back to a string, and compare the lengths of original/parsed strings to see if any character has been stripped, eg:</p>
  
  <pre><code>String original = "050";
  value  = Integer.parseInt( "050", 10);
  String parsed = Integer.toString(value);
  if (original.length() != parsed.length()) {
      System.exit(1);
  }
  </code></pre>
  
  <p>Which works fine, but feels a little hacky. Are there better ways of detecting and handling a leading zero?</p>