java.lang.NumberFormatException.forInputString - java

I'm trying to convert my binary string to decimal value but i'm getting error.
The binary string is of 32 bits so no case of overflow.
String s = "11111111111111111111111111111101";
System.out.print(s.length());
System.out.print(Integer.parseInt(s,2));
Exception is:-
Exception in thread "main" java.lang.NumberFormatException: For input string: "11111111111111111111111111111101"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:583)

Integer type is not big as you expect, instead, try to use BigInteger:
new BigInteger(s, 2)
Output
324294967293

The first bit is 1, so this number cannot be represented as a positive int. If you try, it will "overflow" and become negative.
If that is what you want, you can parse the string as an unsigned value:
System.out.print(Integer.parseUnsignedInt(s,2));
Output: -3

You can use long:
String s = "11111111111111111111111111111101";
System.out.print(s.length());
System.out.print(Long.parseLong(s,2));

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

How do I parse a hex-int from a string to an Integer?

I have no problem in writing this: (it also doesn't give any errors)
int hex = 0xFFFFFFFF;
The integer has to have an alpha value also!
But when I try to do this:
Integer hex = Integer.parseInt("0xFFFFFFFF");
// Or I try this:
Integer hex = Integer.parseInt("FFFFFFFF");
I get a java.lang.NumberFormatException: For input string: "0xFFFFFFFF" thrown!
Why is this not working?
I'm guessing that there is some other way to parse hex integers that I am just not realizing, and I really need to be able to parse hex from string for a program I am making.
There is actually a separate function where you can define the radix:
https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#parseInt(java.lang.String,%20int)
Integer.parseInt(x) simply calls Integer.parseInt(x, 10), so you have to specify a radix 10 number.
In order to parse a hex string, you would simply have to use the following (note that the 0x prefix is not allowed):
Integer.parseInt("FFFFFFF", 16);

Java Fractions, again

Yesterday, I attempted to do this one way...today I am trying another and I am still stuck. I have to find a way of doing this using integer division and mod. Here is my code followed by the error messages.
public int evaluateFraction(int w, int n, int d)
throws NumberFormatException
{
whole = w;
numerator = n;
denominator = d;
return portion2;
}
Tester
input = JOptionPane.showInputDialog("Enter");
portion2 = Integer.parseInt(input);`
error messages:
Exception in thread "main" java.lang.NumberFormatException: For input string: "1 1/8"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at ClientCode.main(ClientCode.java:43)
Java Result: 1
What on earth am I doing wrong now?
Integer.parseInt is able to parse only valid integer strings. If the input string contains anything other than digits then it will throw NumberFormatException.
You are trying to parse an expression 1 1/8, which is not a valid integer string.
"1 1/8" is not a number. 1 and 8 are, whitespace and / are not. You need to parse such expression by hand.
1 1/8 is not an integer, Integer.parseInt can perform well in the only one case, if data is valid.
Don't know, what result you expect but you either need some other method or parse it yourself.

java hexadecimal to int conversion - have to remove 0X ?

I have a file with many hex numbers (for eg - 0X3B4 ). Im trying to parse this file as assign these numbers to integers, but dont seem to get Integer.parseInt to work.
int testint = Integer.parseInt("3B4",16); <- WORKS
int testint = Integer.parseInt("0X3B4",16);
gives error:
Exception in thread "main" java.lang.NumberFormatException: For input string: "0x3b4"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
What is the right way to assign the value 0XB4 to an int ?
Do I have to get rid of the 0X - its not unusual to represent hex nos this way...
You can do
int hex = Integer.decode("0x3b4");
You are right that parseInt and parseLong will not accept 0x or 0X

Converting from hex to int in java

I am getting an error because of the following line of code:
int x = color(Integer.parseInt("ffffffde",16));
I think it might be because it is a minus value
Any ideas why or how or how to fix it?
EDIT:
Sorry, didn't include the actual error. here it is:
Exception in
thread "Animation Thread" java.lang.NumberFormatException: For input
string: "ffffffde" at
java.lang.NumberFormatException.forInputString(Unknown Source) at
java.lang.Integer.parseInt(Unknown Source)
EDIT 2:
The value ("ffffffde") is being created by the following code:
Integer.toHexString(int_val);
EDIT 3:
Turns out it is a known bug (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4215269)
Although you can convert integers to hex strings, you cannot convert them back if they are negative numbers!!
ffffffde is bigger than integer max value
Java int is 32 bit signed type ranges from –2,147,483,648 to 2,147,483,647.
ffffffde = 4,294,967,262
Edit
You used Integer.toHexString(int_val) to turn a int into a hex string. From the doc of that method:
Returns a string representation of the integer argument as an unsigned integer in base 16.
But int is a signed type.
USE
int value = new BigInteger("ffffffde", 16).intValue();
to get it back as a negative value.
If you are getting error like this,
Exception in thread "main" java.lang.NumberFormatException: For input string: "ffffffde"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:461)
at com.TestStatic.main(TestStatic.java:22)
Then there is problem with value you are passing that is ffffffde . This is not a valid hex value for parsing to int.
Please try this
int x = Integer.parseInt("ffffde",16);
System.out.println(x);
It should work.
For hex values more than that you have to pars to Long
Long x = Long.parseLong("ffffffde",16);
System.out.println(x);
And this also should work

Categories