1

How do I cast the value digit in the substring to an integer?

integer check2 = nif.substring(1,1) * 8;

2 Answers 2

7

You can't cast String to Integer as String is never an instance of Integer

In general to cast one type to another, cover object that should be cast to another type with brackets

Object check2 = ((Object)nif.substring(1,1));

I recommend you to use Integer.valueOf method

Returns an Integer that contains the value of the specified String. As in Java, the String is interpreted as representing a signed decimal integer.

Integer check2 = Integer.valueOf(nif.substring(1,1)) * 8;
0
2
Integer check2 = Integer.valueOf(nif.substring(1,1)) * 8;

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .