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.
Related
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
This block of code gives Number Format Exception on input 600000 in n
import java.util.*;
class SpoTwo{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int testcase,n,answer;
long bin;
String s;
testcase=sc.nextInt();
for(int i=0;i<testcase;i++){
n=sc.nextInt();
s=Integer.toBinaryString(n);
bin=Integer.parseInt(s);
answer=(int)Math.pow(2,bin*2);
System.out.println(answer%1000000007);
}
}
}
Exception:
Exception in thread "main" java.lang.NumberFormatException: For input string: "10010010011111000000"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:495)
at java.lang.Integer.parseInt(Integer.java:527)
at SpoTwo.main(SpoTwo.java:12)
The binary representation of 600000 is 10010010011111000000. This is not a valid base 10 integer. It is a valid base 2 integer. Use
bin = Integer.parseInt(s, 2);
Here's the method's javadoc.
The overloaded parseInt method you were using
Parses the string argument as a signed decimal integer. The characters
in the string must all be decimal digits, except that the first
character may be an ASCII minus sign '-' ('\u002D') to indicate a
negative value or an ASCII plus sign '+' ('\u002B') to indicate a
positive value. The resulting integer value is returned, exactly as if
the argument and the radix 10 were given as arguments to the
parseInt(java.lang.String, int) method.
you got a string and move to binary and then converts that string to an integer and then vc is the power, so that the whole supports a certain number of houses, so it's a blast, u can use paserInt putting the number of homes or turns the whole into a double.
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
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
like "1+23"
parse to double for example.. then calculate..
but when i have a decimal the program crashes for example "1.1+2" the program glitches on the 1.1 when i'm parsing it
if(s.contains("+"))
{
int n = s.indexOf("+");
String w1 = s.substring(0,n);
String w2 = s.substring(n+1,s.length());
part1= (long) Double.parseDouble(w1);
part2 = (long)Double.parseDouble(w2);
Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1.1"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
You probably use Integer.parseInt(). If that's not the case, then you need to specify a lot more detail (how does it crash, post the stack trace, post the code).
You need to switch to either floating point numbers (Double.parseDouble()) or to BigDecimal (new BigDecimal()).
why are you casting from a double to long? that will result in your number being truncated. in the case of 1.1 you will get 1. What you should do is use the following:
Float.parseFloat(w1);
where w1 is your floating point value.
you cannot have part1 be a long value, since long values cannot hold floating points. part1 must be a float.