It's seems like integer but it not parse with Integer.parse() How can I fix it ?
int a= Integer.parseInt(lhs.getViewCount());
Caused by: java.lang.NumberFormatException: Invalid int: "6125635424"
at java.lang.Integer.invalidInt(Integer.java:138)
at java.lang.Integer.parse(Integer.java:378)
at java.lang.Integer.parseInt(Integer.java:366)
at java.lang.Integer.parseInt(Integer.java:332)
at com.youtubetracker.adapters.MyChannelListAdapter$1.compare(MyChannelListAdapter.java:46)
at com.youtubetracker.adapters.MyChannelListAdapter$1.compare(MyChannelListAdapter.java:37)
Always use BigInteger over long and float.
BigInteger value= new BigInteger(lhs.getViewCount());
You can convert that value to int , long or float.
Form Java Doc intValue()
Converts this BigInteger to an int. This conversion is analogous to a narrowing primitive conversion from long to int as defined in section 5.1.3 of The Java™ Language Specification: if this BigInteger is too big to fit in an int, only the low-order 32 bits are returned. Note that this conversion can lose information about the overall magnitude of the BigInteger value as well as return a result with the opposite sign.
The maximum Range of Integer is 2147483647 and you are converting 6125635424 to integer. This is the main reason behind the Exception.
Long.parseLong(lhs.getViewCount());
Will solve your problem.
Try this :
Replace: int a= Integer.parseInt(lhs.getViewCount());
With
Long a= Long.parseInt(String.valueOf(lhs.getViewCount()));
'6125635424' cannot store in Integer you need to store in Double or Long.
Try this
Long a = Long.parseLong(lhs.getViewCount().toString());
Related
I have this line of Java code that will throw NumberFormatException if the number represented as a String is above 2,147,483,647.
Because:
The int data type is a 32-bit signed two's complement integer. It has
a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647
Code Throwing the NumberFormatException:
String largeNumberAsAString = "9999999999";
Integer.toHexString(Integer.parseInt(largeNumberAsAString)); // NumberFormatException
How can I get the same functionality of theInteger.toHexString() with a String parameter and not an int parameter due to NumberFormatException?
Use BigInteger to avoid numeric limits of primitive int and long:
BigInteger x = new BigInteger("9999999999999999999999"); // Default radix is 10
String x16 = x.toString(16); // Radix 16 indicates hex
System.out.println(x16);
The class conveniently exposes a constructor that takes a String, which gets interpreted as a decimal representation of a number.
Demo.
If your input value can be arbitrarily large, then #dasblinkenlight's answer involving BigInteger is your best bet.
However, if your value is less than 263, then you can just use Long instead of Integer:
String dec = "9999999999";
String hex = Long.toHexString(Long.parseLong(dec));
System.out.println(hex); // 2540be3ff
Live demo.
Use Integer.parseUnsignedInt
When the number is above 2^31 but below 2^32, thus in the negative int range,
you can do:
int n = Integer.parseUnsignedInt("CAFEBABE", 16);
(I used hexadecimal here, as it is easier to see that above we are just in that range.)
However 9_999_999_999 is above the unsigned int range too.
Try this way:
String largeNumberAsAString = "9999999999";
System.out.println(Integer.toHexString(BigDecimal.valueOf(Double.valueOf(largeNumberAsAString)).intValue()));
I have a question about the promotion of primitive types in Java. As we can see in the following example, one of the methods does not compile due to an error of type mismatch. Each method returns the same value but in different types.
The version of primitive long method works without error while the version of wrapper class Long fails. This is because the int literal in the return statement will be first promoted to a broader primitive type (e.g. long) and then to the corresponding wrapper class Integer and so on. Since Integer is not a subclass of Long the compiler gives an error.
But why does the version of wrapper class Byte works without any error? What exactly does the compiler do at this point?
long getPrimitiveLong() {
return 12; // valid
}
Long getWrapperLong() {
return 12; // Error: type mismatch
}
Byte getWrapperByte() {
return 12; // valid
}
The version with Byte works through some compiler magic.
Unlike long numeric literals which can be constructed with a L suffix, e.g. 12L, there is no such thing as a byte literal. That is why Java compiler treats numeric literals that fit in a byte as byte literals. Hence, 12 in your last example is considered a constant of type byte.
Java Language Specification offers a description of this conversion in section 5.2:
A narrowing primitive conversion followed by a boxing conversion may be used
if the type of the variable is:
Byte and the value of the constant expression is representable in the type byte.
Short and the value of the constant expression is representable in the type short.
Character and the value of the constant expression is representable in the type char.
This is because Java allows 1 conversion or Autoboxing, not more.
Java can do all these:
int i = 5;
double d = i; // int to double
long l = i; // int to long
long l = d; // double to long
Or autobox:
Integer i = 5; // int to Integer
Double d = 5.0; // double to Double
Long l = 5L; // long to Long
Converting twice, say int to Double, gives Java a hard time.
number like 12 consider as int by default by the compiler that is why the error
To fix that you can use casting for byte and place L after the value of long variable.
Read following post for more details
http://javaseeeedu.blogspot.com/2015/12/casting-part-1.html
As a short answer - try to replace 12 with 128 (byte is in range -128 to 127).
It won't compile, right?
The outcome here is that the compiler knows about byte boundaries.
For the in-depth answer you can do a deep dive into OpenJDK.
I have a number of 20 digit, which datatype will support to store this number? I have tried long, double but I 'm getting out of range.
Number = 48565664968483514466
Then I have to convert this number to Base36 to generate the barcode.
BigInteger:
The BigInteger class allocates as much memory as it needs to hold all the bits of data it is asked to hold and also provides operations analogues to all of Java's primitive integer operators and for all relevant methods from java.lang.Math.
Declare it as
BigInteger bi1 = new BigInteger("12345678900123");
To convert your number in base 36:
BigInteger number = new BigInteger("48565664968483514466");
String numberInBase36 = number.toString(36);
System.out.println(numberInBase36);
when I'm trying to use new BigInteger(number), I'm getting literal of type int is out of range
The syntax you are looking for is
BigInteger n = new BigInteger("48565664968483514466");
with the numeric literal as a String, since the primitive integer literals cannot hold a number this large.
BigInteger i = new BigInteger("48565664968483514466");
Why does this code throw NumberFormatException?
int a = Integer.parseInt("1111111111111111111111111111111111111111");
How to get the value of int for that String?
The value that you're attempting to parse is much bigger than the biggest allowable int value (Integer.MAX_VALUE, or 2147483647), so a NumberFormatException is thrown. It is bigger than the biggest allowable long also (Long.MAX_VALUE, or 9223372036854775807L), so you'll need a BigInteger to store that value.
BigInteger veryBig = new BigInteger("1111111111111111111111111111111111111111");
From BigInteger Javadocs:
Immutable arbitrary-precision integers.
This is because the number string is pretty large for an int . Probably this requires a BigInteger .
There is no integer value for that string. That's why it's throwing an exception. The maximum value for an integer is 2147483647, and your value clearly exceeds that.
I currently read from a ByteBuffer a short value and apply it to an int.
byte[] data = new byte{0x90, 0xAF};
ByteBuffer b = ByteBuffer.wrap(data);
int value = b.getShort();
but the value contains now 0xFFFF90AF and not 0x90AF.
My solution is to bitmask the value by 0xFFFF: int value = b.getShort() & 0xFFFF;
I thought that a upcast is always possible, because short is smaller than int. Can someone explain why it behaves like this?
short is signed, and that is preserved when casting to int. 0x90AF is a negative short, so the result is negative int. Your solution of masking it is correct.
short is a signed quantity, but you are in luck: there is also char, which is the only unsigned primitive type in Java, and is the same size as short. Try to use
int value = b.getChar();
It is an often neglected fact that char is a full-blown numeric type in Java; it is only its string representation which betrays its special status.