Problems parsing String to long - what am I doing wrong? - java

Here is the code I am working with in a main method:
String numbers = "12345678900";
long upc = Integer.parseInt(numbers);
System.out.println(upc);
gives me:
Exception in thread "main" java.lang.NumberFormatException: For input string: "12345678900"
at java.lang.NumberFormatException.forInputString...
at java.lang.Integer.parseInt....
at java.lang.Integer.parseInt...
at testRun.main...
I cannot use a double, they need to be stored as values without a decimal. I am trying to get the string of numbers from a string into a variable that holds numbers (no decimals)

To parse a long, use Long.parseLong, not Integer.parseInt. That way, you get access to the full range of long values (whereas with parseInt, you only get the rather more restricted range of int values).

Use Long.parseLong()
String numbers = "12345678900";
long upc = Long.parseLong(numbers);
System.out.println(upc);

use
String numbers = "12345678900";
long upc = Long.parseLong(numbers);
System.out.println(upc);

The number you are passing is outside the range of integer which is from -2,147,483,648 to 2,147,483,647.
Try using the static function parseLong

Your number is enough long to not fit in Integer. So, you can't cast in integer. You can cast it in Long by calling Long.parseLong(number).

Use
long upc = Long.parseLong(numbers);

Related

number format exception when trying to parse a string

I want to convert Integer.MAX_VALUE to binary and want the represenation to be of type int. I passed it to Integer.tobinarystring() and wraped that with Integer.parseint but i get numberformatexception.
Here is the code
System.out.println(
Integer.parseInt(
Integer.toBinaryString(Integer.MAX_VALUE)
)
);
Here is the exception
Exception in thread "main" java.lang.NumberFormatException: For input string: "1111111111111111111111111111111"
Integer.MAX_VALUE is 2,147,483,647
In binary this is:
1111111111111111111111111111111
If we treat that like an integer again, which is 1,111,111,111,111,111,111,111,111,111,111 you can probably see that it is much larger than the max value.
You probably want to look into BigInteger if you really need to deal with that as a int.
If you want to get the integer value of the binary string
1111111111111111111111111111111
you must use another signature of parseInt() that takes as 2nd parameter the radix, in this case of a binary string the radix is 2
String str = Integer.toBinaryString(Integer.MAX_VALUE);
int number = Integer.parseInt(str, 2);
System.out.println(number);
it will print:
2147483647

NumberFormatException when converting Integer String in Java

I declare this variable:
private String numCarteBancaireValide=String.valueOf(((Integer.parseInt(Config.NUM_CARTE_BANCAIRE_VALIDE) ) + (Integer.parseInt("0000000000000001"))));
Config.NUM_CARTE_BANCAIRE_VALIDE is a string.
After Execution, I receive this error message :
java.lang.NumberFormatException: For input string: "4111111111111111"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:583)
at java.lang.Integer.parseInt(Integer.java:615)
Please, Can you give your advices ?
Use Long.parseLong(), as your parameter is too large for an Integer. (Maximum for an integer is 2147483647)
PS: using Integer.parseInt("0000000000000001") doesn't make much sense either, you could replace this with 1.
The 4111111111111111 (which most likely is the value of Config.NUM_CARTE_BANCAIRE_VALIDE) overflows the Integer type.
Better try with:
//if you need a primitive
long value = Long.parseLong(Config.NUM_CARTE_BANCAIRE_VALIDE);
or
//if you need a wrapper
Long value = Long.valueOf(Config.NUM_CARTE_BANCAIRE_VALIDE);
The maximum value of integer is 2147483647. So you need to use Long.parseLong instead to parse 4111111111111111. Something like this:
long l = Long.parseLong("4111111111111111");
On a side note:
As Alex has commented, if this number is representing a credit card number then you can treat it like a string instead of changing to long as there is no arithmetic calculations involved with the credit card numbers.
Integer.parseInt will attempt to parse an integer from a String.
Your "4111111111111111" String does not represent an valid Java integer type, as its value would be > Integer.MAX_VALUE.
Use Long.parseLong instead.

Convert hexadecimal string to hexadecimal integer in java

I have hexadecimal String eg. "0x103E" , I want to convert it into integer.
Means String no = "0x103E";
to int hexNo = 0x103E;
I tried Integer.parseInt("0x103E",16); but it gives number format exception.
How do I achieve this ?
You just need to leave out the "0x" part (since it's not actually part of the number).
You also need to make sure that the number you are parsing actually fits into an integer which is 4 bytes long in Java, so it needs to be shorter than 8 digits as a hex number.
No need to remove the "0x" prefix; just use Integer.decode instead of Integer.parseInt:
int x = Integer.decode("0x103E");
System.out.printf("%X%n", x);

NumberFormatException in InputString

Please help me solve this exception:-
String strBinary="100000000000000001000000000000000000000000000000000000000000000000000000";
System.out.println("length is " + strBinary.length());
long intParse=Long.parseLong(strBinary, 2);
System.out.println("int parsed is " + intParse);
String hexString=Long.toHexString(intParse);
System.out.println(hexString);
Output is 72 along with NumberFormatException while parsing using Long.parseLong..
But till yesterday it was running absolutely fine for this input also..
does it have anything to do with the length...
I am actuallly trying to convert String into its equivalent Hex value.
Please help....
A long can hold 64 bit of data. The biggest value a long can represent is 9223372036854775807 (or 263-1). The string you try to parse is a lot larger than that.
You might be able to go somewhere by using the BigInteger class, which can handle arbitrary-sized integer values (effectively restricted by memory, of course).
Long is small for your purpose. You might want to use BigInteger object like this
String strBinary="100000000000000001000000000000000000000000000000000000000000000000000000";
BigInteger bigInteger = new BigInteger(strBinary, 2);
System.out.println(bigInteger.longValue()); //This would give you the long value
System.out.println(bigInteger.toString(16)); //This would give you the hex string

Is J2ME's Integer.parseInt() broken?

While writing a game for J2ME we ran into an issue using java.lang.Integer.parseInt()
We have several constant values defined as hex values, for example:
CHARACTER_RED = 0xFFAAA005;
During the game the value is serialized and is received through a network connection, coming in as a string representation of the hex value. In order to parse it back to an int we unsuccesfully tried the following:
// Response contains the value "ffAAA005" for "characterId"
string hexValue = response.get("characterId");
// The following throws a NumberFormatException
int value = Integer.parseInt(hexValue, 16);
Then I ran some tests and tried this:
string hexValue = Integer.toHexString(0xFFAAA005);
// The following throws a NumberFormatException
int value = Integer.parseInt(hexValue, 16);
This is the exception from the actual code:
java.lang.NumberFormatException: ffaaa005
at java.lang.Integer.parseInt(Integer.java:462)
at net.triadgames.acertijo.GameMIDlet.startMIDlet(GameMIDlet.java:109)
This I must admit, baffled me. Looking at the parseInt code the NumberFormatException seems to be thrown when the number being parsed "crosses" the "negative/positive boundary" (perhaps someone can edit in the right jargon for this?).
Is this the expected behavior for the Integer.parseInt function? In the end I had to write my own hex string parsing function, and I was quite displeased with the provided implementation.
In other words, was my expectation of having Integer.parseInt() work on the hex string representation of an integer misguided?
EDIT: In my initial posting I wrote 0xFFFAAA005 instead of 0xFFAAA005. I've since corrected that mistake.
The String you are parsing is too large to fit in an int. In Java, an int is a signed, 32-bit data type. Your string requires at least 36 bits.
Your (positive) value is still too large to fit in a signed 32-bit int.
Do realize that your input (4289372165) overflows the maximum size of an int (2147483647)?
Try parsing the value as a long and trim the leading "0x" off the string before you parse it:
public class Program {
public static void main(String[] args) {
String input = "0xFFFAAA005";
long value = Long.parseLong(input.substring(2), 16);
System.out.print(value);
}
}
I'm not a java dev, but I'd guess parseInt only works with integers. 0xFFFAAA005 has 9 hex digits, so it's a long, not an int. My guess is it's complaining because you asked it to parse a number that's bigger than it's result data type.
Your number seems to be too large to fit in an int, try using Long.parseLong() instead.
Also, the string doesn't seem to get parsed if you have 0x in your string, so try to cut that off.

Categories