java.lang.NumberFormatException: Invalid int: “3546504756”, what does this error mean?

核能气质少年 提交于 2019-12-24 00:16:57

问题


I'm creating an Android app, and I'm reading some coordinates from a text file.

I'm using Integer.parseInt(xCoordinateStringFromFile) to convert the X coordinates to integers, and in the same way with the Y coordinates.

When I run the app, I get an error on that line, which looks like this:

BridgeData data = new BridgeData(
    elements[0],
    elements[1],
    Integer.parseInt(elements[2]),
    Integer.parseInt(elements[3]),
    Integer.parseInt(elements[4]),
    new GeoPos(Integer.parseInt(elements[5].split(",")[0]), Integer.parseInt(elements[5].split(",")[1])),
    new GeoPos(Integer.parseInt(elements[6].split(",")[0]), Integer.parseInt(elements[6].split(",")[1])),
    Integer.parseInt(elements[7]),
    Integer.parseInt(elements[8])
);

The variable elements is a String array created by splitting the current line on every ;.

The "main" error is:

java.lang.NumberFormatException: Invalid int: "3546504756"

I wonder what this means, and how I can solve it.


回答1:


Error just means that java is not able to convert the String that you are trying to use in your call to Integer.pasrseInt as that number is out of range of an integer.

You should be using Long.parseLong as 3546504756 number is out of range of an integer.

Make sure post that your BridgeData constructor accepts long as a parameter instead of integer.




回答2:


Revising the concept of data type and their size might help you

http://www.tutorialspoint.com/java/java_basic_datatypes.htm




回答3:


In Java, an int is 32 bits, which is enough to store numbers up to just over 2 billion. The number you were trying to read was an invalid int because it was too big.

I would seriously question the design of whatever you are doing, if you have coordinates with values of over a billion. But if you really need such big numbers, use long in place of int in your BridgeData class, and Long.parseLong in place of Integer.parseInt in the code that you quoted.




回答4:


The range of int value can be lies between -2,147,483,648 to 2,147,483,647 and you are providing it more than that thats why it giving numberformatexception

You have to store the value in either long or other more range premetive type.

You can find more about java premetive data type range and value here



来源:https://stackoverflow.com/questions/27331336/java-lang-numberformatexception-invalid-int-3546504756-what-does-this-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!