问题
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?
回答1:
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:
The construct cases2[0]
will pick the first character out of the cases2
string.
Integer.parseInt()
requires a String
argument, not a char
.
来源:https://stackoverflow.com/questions/29583655/java-thinks-i-want-to-convert-char-to-string-using-integer-parseint