0

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

4 Answers4

1

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

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Noir
  • 78
  • 1
  • 5
1

You can use this to convert your String to int.

String str = "-10d";
int i = Double.valueOf(str).intValue();

Hope this helps!

anacron
  • 6,443
  • 2
  • 26
  • 31
1

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());
wake-0
  • 3,918
  • 5
  • 28
  • 45
1

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);
Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140