SafeParse { public static Integer stringToInt(String input) { Integer result = 0; input = input.replace(",", "."); try { result = Integer.parseInt(input); } catch (Exception e) { } return result; }  static Double stringToDouble(String input); static Integer stringToInt(String input); static Long stringToLong(String input); static Float stringToFloat(String input);  }
@Test public void stringToIntTest() { assertThat(1052934, is(SafeParse.stringToInt("1052934"))); assertThat(-42, is(SafeParse.stringToInt("-42"))); assertThat(2147483647, is(SafeParse.stringToInt("2147483647"))); assertThat(-2147483648, is(SafeParse.stringToInt("-2147483648"))); assertThat(0, is(SafeParse.stringToInt("2147483648"))); assertThat(0, is(SafeParse.stringToInt("-2147483649"))); assertThat(0, is(SafeParse.stringToInt("12 U/h"))); assertThat(0, is(SafeParse.stringToInt("0.1234"))); assertThat(0, is(SafeParse.stringToInt("0,1234"))); assertThat(0, is(SafeParse.stringToInt(".5436564812"))); assertThat(0, is(SafeParse.stringToInt(",5436564812"))); assertThat(0, is(SafeParse.stringToInt("42.1234"))); assertThat(0, is(SafeParse.stringToInt("42,1234"))); assertThat(0, is(SafeParse.stringToInt("3212.5436564812"))); assertThat(0, is(SafeParse.stringToInt("3212,5436564812"))); assertThat(0, is(SafeParse.stringToInt("1000500100900"))); assertThat(0, is(SafeParse.stringToInt("ala ma kota"))); assertThat(0, is(SafeParse.stringToInt(""))); assertThat(0, is(SafeParse.stringToInt(" "))); assertThat(0, is(SafeParse.stringToInt("\n\r"))); }
@Test(expected=NullPointerException.class) public void stringToIntNullTest() { SafeParse.stringToInt(null); }