-1

What is going on here?

This is the value of checkSum = "2367122119". And I would like to parse this number as an integer value like this:

int ipAddressAsInt = Integer.parseInt(checkSum.trim());

As a result I'm getting the following exception:

java.lang.NumberFormatException: For input string: "2367122119"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:583)
    at java.lang.Integer.parseInt(Integer.java:615)
    at com.example.servlets.RDServlet.doPost(RDServlet.java:40)
    ...

Also, the same happens if I try Long.parseLong(checkSum).

How is that possible?

toom
  • 12,864
  • 27
  • 89
  • 128
  • Please show a short but complete program demonstrating the problem, including when you use `Long.parseLong`, bearing in mind that the value you've shown *is* too big for `int`... – Jon Skeet Dec 10 '15 at 14:00
  • 2
    Could it be that you forgott to change the type of ipAddressAsInt to long and you make a cast? – nano_nano Dec 10 '15 at 14:03
  • @StefanBeike: In that case it wouldn't compile - you can't assign a `long` value to an `int` variable. – Jon Skeet Dec 10 '15 at 14:06
  • Perhaps change the title? "java.lang.NumberFormatException when trying to parse a number greater than Integer.MAX_VALUE as an int" – Erick G. Hagstrom Dec 10 '15 at 14:07
  • @Jon Skeet: I was faster with my edit ;-) – nano_nano Dec 10 '15 at 14:07
  • @StefanBeike: Okay, then the OP *still* wouldn't get "the same" result - they wouldn't have an exception at all, just a truncation to `int`. – Jon Skeet Dec 10 '15 at 14:08
  • 2
    Okay, that is a valid question and I accepted an anwser. But why am I getting downvotes for it??? I do not see any justification for a downvote?! Although I do not care about this, I'm wondering why people would downvote a normal question? – toom Dec 10 '15 at 14:16

3 Answers3

2

the number is to big for an int:

@Test
public void testMaxInt() {
    System.out.println(Integer.MAX_VALUE);   

    System.out.println(Integer.MIN_VALUE);   

  }

2147483647

-2147483648

nano_nano
  • 12,351
  • 8
  • 55
  • 83
1

The number is too big for an integer. Using Long it should definitely work though. Try it like this:

  String checkSum = "2367122119";
  long ipAddressAsInt = Long.parseLong(checkSum.trim());
  System.out.println(ipAddressAsInt);
eol
  • 23,236
  • 5
  • 46
  • 64
0

The number exceeds the size of the int container. You can use long but you would have to declare the variable as a long also:

long ipAddressAsInt = Long.parseLong(checkSum.trim());

this should work in theory....

Anton
  • 422
  • 2
  • 9
  • Thanks for the answer. However, in case of parsing an ip address this way you really should not use it because it makes problems if you try to parse (for instance) `0.0.0.0` and also some other addresses. For more look here: http://stackoverflow.com/questions/1957637/java-convert-int-to-inetaddress – toom Dec 11 '15 at 11:10
  • For an ip address an array seems the logical choice but it really comes down to what you want to do with the data. An array(you could use byte or int as the type if you wish) would remove the problem of parsing 0.0.0.0 for example but its not going to be as easy to handle and use as a single primitave value. The link you posted actually does this in a nicer way than I was considering... – Anton Dec 11 '15 at 11:23