Java thinks I want to convert char to String using Integer.parseInt()

柔情痞子 提交于 2019-12-12 06:20:17

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!