0

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

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

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;
| improve this answer | |
2
Integer check2 = Integer.valueOf(nif.substring(1,1)) * 8;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.