Reputation: 47
I can't cast this value as Integer
and I can't file what type is (I know is String
) but I need convert as number, Interger.parse()
does not work I get Exception
.
String str = "-10d";
Interger.parse(str); //I get Exception
Upvotes: 0
Views: 3029
Reputation: 3968
Here you get the double
value:
String str = "-10d";
Double d = Double.parseDouble(str);
System.out.println(d);
And to get the integer value:
int intValue = d.intValue();
Other solutions, which change the input string
for the parse
method are (I don't recommend them):
int x = Integer.valueOf(str.substring(0, str.length() - 1));
int y = Integer.valueOf(str.replace('d', ' ').trim());
Upvotes: 1
Reputation: 59968
You get Exception because it is not correct there are no Interger.parse(str);
i think you want to make :
Integer.parseInt(str);
But your string have d
and d work with Double not with Integer so instead use this :
String str = "-10d";
Double.parseDouble(str);
Upvotes: 1
Reputation: 78
String str = "-10b"
(mentioned in question) or "-10d" (mentioned in title)?
If what you mean is -(number)d,then it is double.
try
double b = Double.parseDouble("-10d");
instead of using Integer.parseInt
Upvotes: 1
Reputation: 6721
You can use this to convert your String
to int
.
String str = "-10d";
int i = Double.valueOf(str).intValue();
Hope this helps!
Upvotes: 1