0

I get the error message error: incompatible types: char cannot be converted to String return Integer.parseInt(cases2[0]);

but my code is:

public static int standingOvation(String cases){

    char[] cases2 = cases.toCharArray();
    return Integer.parseInt(cases2[0]);
}

Why am I getting the error if i'm clearly trying to convert cases (which is passed in as "11111") to an integer?

2 Answers 2

3

The construct cases2[0] will pick the first character out of the cases2 string.

Integer.parseInt() requires a String argument, not a char.

Sign up to request clarification or add additional context in comments.

2 Comments

got it. so I guess I should use Character.getNumericValue(cases2[0]) instead?
@ralphie9224 Yes, you should.
2

Try

return Integer.parseInt("" + cases2[0]);

By adding to the empty string "" you convert to a string, which is the right type for Integer.parseInt.

2 Comments

@ralphie9224 You can also do String.valueOf(cases2[0]) to convert a char to a String, but the "" + trick is useful to know as it converts anything to a String. It is a bit hacky though.
@pbabcdefp String.valueOf also converts anything to String.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.