String to Int in java - Likely bad data, need to avoid exceptions

前端 未结 16 2001
迷失自我
迷失自我 2020-12-04 18:02

Seeing as Java doesn\'t have nullable types, nor does it have a TryParse(), how do you handle input validation without throwing an exceptions?

The usual way:

16条回答
  •  时光取名叫无心
    2020-12-04 18:42

    Try org.apache.commons.lang.math.NumberUtils.createInteger(String s). That helped me a lot. There are similar methods there for doubles, longs etc.

提交回复
热议问题