This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 5 years ago.
I have written a function to convert string to integer
if ( data != null )
{
int theValue = Integer.parseInt( data.trim(), 16 );
return theValue;
}
else
return null;
I have a string which is 6042076399 and it gave me errors:
Exception in thread "main" java.lang.NumberFormatException: For input string: "6042076399"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:461)
Is this not the correct way to convert string to integer?
Here's the way I prefer to do it:
Edit (08/04/2015):
As noted in the comment below, this is actually better done like this:
String numStr = "123";
int num = Integer.parseInt(numStr);
An Integer can't hold that value. 6042076399 (413424640921 in decimal) is greater than 2147483647, the maximum an integer can hold.
Try using Long.parseLong.
That's the correct method, but your value is larger than the maximum size of an int.
The maximum size an int can hold is 231 - 1, or 2,147,483,647. Your value is 6,042,076,399. You should look at storing it as a long if you want a primitive type. The maximum value of a long is significantly larger - 263 - 1. Another option might be BigInteger.
That string is greater than Integer.MAX_VALUE. You can't parse something that is out of range of integers. (they go up to 2^31-1, I believe).
In addition to what the others answered, if you have a string of more than 8 hexadecimal digits (but up to 16 hexadecimal digits), you could convert it to a long using Long.parseLong() instead of to an int using Integer.parseInt().
Related
This question already has answers here:
Convert an integer to an array of digits
(24 answers)
Closed 2 years ago.
I would just like to ask how to convert an int to int array - for example:
int number = 12345;
to:
[1,2,3,4,5];
Btw - could not really find anything out there, so we may hope that someone know.
thanks in advance
You can do it like this:
int number = 12345;
int[] digits = String.valueOf(number).chars().map(c -> c-'0').toArray();
Explanation:
First convert int to string with String.valueOf(number) to be able to use the method chars() which get a stream of int that represents the ASCII value of each char in the string. Then, we use the map function map(c -> c-'0') to convert the ASCII value of each character to its value, subtracting the value of the ASCII code from the character '0' from the ASCII code of the actual character. Finally, the stream is converted to an array with toArray().
The following code throws NumberFormatException and I don't understand why,
String sku = "008949679851";
System.out.println(Integer.valueOf(sku));
Interestingly, if I remove the first three digits and the keep the input string as "949679851", then this exception is not thrown. Is there a limit in length when converting a string to an integer value..? How can I make it work with the full string..?
Because the max value of an Integer is Integer.MAX_VALUE = 2147483647 and your number is greater than this 8949679851. Instead use Long.valueOf(sku) or BigInteger for example:
Long l = Long.valueOf(sku);//Max value equal to 9223372036854775807
BigInteger b = new BigInteger(sku);
In Java the maximum value for int and Integers is 2^31-1 (2147483647) so your number exceeds that value.
Java integer size is 32 bits (range -2,147,483,648 to +2,147,483,647). "008949679851" is too long, while "949,679,851" is within the range.
This question already has answers here:
java.lang.NumberFormatException: For input string
(7 answers)
Closed 5 years ago.
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
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));
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.
In my java code, I have a string 9632580147, and when I convert it into a int, using this code:
try{
sNumberInt = Integer.parseInt(sNumber);
} catch(NumberFormatException nfe) {
Log.d("NUMBER", nfe.getMessage());
return;
}
It goes into the catch block saying Invalid int: "9632580147"...
Does anyone know how to fix this?
Thanks
When you type
int sNumber = 9632580147;
into your code, the compiler will tell you:
The literal 9632580147 of type int is out of range
The reason is that your number is too big to fit into an int, use a long instead.
Max value of int is 2147483647 and you are trying to pass 9632580147 which is greater. Try maybe Long.parseLong(sNumber)
It seems you are passing value larger than 32 bits:
9632580147 = 1000111110001001011000001000110011 (34 bits)
Integer max value is 2147483647. If you want to part that number, you need to parse it into a Long.
The maximum value of the integer in Java is 2147483647 while your input 9632580147 is greater. Instead, use a long data type:
long sNumberLong = Long.parseLong(sNumber);
I'm currently trying to parse some long values stored as Strings in java, the problem I have is this:
String test = "fffff8000261e000"
long number = Long.parseLong(test, 16);
This throws a NumberFormatException:
java.lang.NumberFormatException: For input string: "fffff8000261e000"
However, if I knock the first 'f' off the string, it parses it fine.
I'm guessing this is because the number is large and what I'd normally do is put an 'L' on the end of the long to fix that problem. I can't however work out the best way of doing that when parsing a long from a string.
Can anyone offer any advice?
Thanks
There's two different ways of answering your question, depending on exactly what sort of behavior you're really looking for.
Answer #1: As other people have pointed out, your string (interpreted as a positive hexadecimal integer) is too big for the Java long type. So if you really need (positive) integers that big, then you'll need to use a different type, perhaps java.math.BigInteger, which also has a constructor taking a String and a radix.
Answer #2: I wonder, though, if your string represents the "raw" bytes of the long. In your example it would represent a negative number. If that's the case, then Java's built-in long parser doesn't handle values where the high bit is set (i.e. where the first digit of a 16 digit string is greater than 7).
If you're in case #2, then here is one (pretty inefficient) way of handling it:
String test = "fffff8000261e000";
long number = new java.math.BigInteger(test, 16).longValue();
which produces the value -8796053053440. (If your string is more than 16 hex digits long, it would silently drop any higher bits.)
If efficiency is a concern, you could write your own bit-twiddling routine that takes the hex digits off the end of the string two at a time, perhaps building a byte array, then converting to long. Some similar code is here:
How to convert a Java Long to byte[] for Cassandra?
The primitive long variable can hold values in the range from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 inclusive.
The calculation shows that fffff8000261e000 hexademical is 18,446,735,277,656,498,176 decimal, which is obviously out of bounds. Instead, fffff8000261e000 hexademical is 1,152,912,708,553,793,536 decimal, which is as obviously within bounds.
As everybody here proposed, use BigInteger to account for such cases. For example, BigInteger bi = new BigInteger("fffff8000261e000", 16); will solve your problem. Also, new java.math.BigInteger("fffff8000261e000", 16).toString() will yield 18446735277656498176 exactly.
The number you are parsing is too large to fit in a java Long. Adding an L wouldn't help. If Long had been an unsigned data type, it would have fit.
One way to cope is to divide the string in two parts and then use bit shift when adding them together:
String s= "fffff8000261e000";
long number;
long n1, n2;
if (s.length() < 16) {
number = Long.parseLong(s, 16);
}
else {
String s1 = s.substring(0, 1);
String s2 = s.substring(1, s.length());
n1=Long.parseLong(s1, 16) << (4 * s2.length());
n2= Long.parseLong(s2, 16);
number = (Long.parseLong(s1, 16) << (4 * s2.length())) + Long.parseLong(s2, 16);
System.out.println( Long.toHexString(n1));
System.out.println( Long.toHexString(n2));
System.out.println( Long.toHexString(number));
}
Note:
If the number is bigger than Long.MAX_VALUE the resulting long will be a negative value, but the bit pattern will match the input.