1

i have following numbers saved in array (readed from XML files):

100000000000008261
100000000000008266
100000000000008267
100000000000008268

The SeqNrList is filled by this:

ArrayList SeqNrList = new ArrayList<>();
SeqNrList.add(doc.getElementsByTagName("SequenceNumber").item(0).getTextContent());

I've try to get the minimum and maximum value with following code:

int seqsizemin = Integer.parseInt((String) Collections.min(SeqNrList));
int seqsizemax = Integer.parseInt((String) Collections.max(SeqNrList));

Also, i've try'd with following:

int seqsizemin = Integer.valueOf((String) Collections.min(SeqNrList));
int seqsizemax = Integer.valueOf((String) Collections.max(SeqNrList));

But i got only following error when i run my script:

Exception in thread "main" java.lang.NumberFormatException: For input string: "100000000000008261"
    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 ReadXMLFile.main(ReadXMLFile.java:117)

Is there any special function needed, why i cant save

2
  • the numbers are probably too large for Integers to handle. Try using Long class instead. Commented Nov 9, 2017 at 21:26
  • long min = SeqNrList.stream().mapToLong(Long::parseLong).min(); (Your idea would have worked, when the numbers indeed all have the same number of digits.) Commented Nov 9, 2017 at 21:40

2 Answers 2

0

According to the JavaDoc of Integer#parseInt():

An exception of type NumberFormatException is thrown if any of the following situations occurs: [...] The value represented by the string is not a value of type int.

Any number which can not be parsed into an int is invalid.

In your case is the number 100000000000008261 larger than the 32-Bit Integer of Java. That's why you're getting the error.

To go arround this, have a look at Long#parseLong():

long seqsizemin = Long.parseLong((String) Collections.min(SeqNrList));
long seqsizemax = Long.parseLong((String) Collections.max(SeqNrList));
Sign up to request clarification or add additional context in comments.

Comments

0

You need to remember that integer data types can hold value requiring up to 32 bits, the values from your example require more than 32 bits to be represented, using Long.parseLong could give you the value represented as long and in case you need to handle bigger values, take a look to BigInteger.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.