Java 认为我想使用 Integer.parseInt() 将 char 转换为 String
Java thinks I want to convert char to String using Integer.parseInt()
我收到错误消息 error: incompatible types: char cannot be converted to String
return Integer.parseInt(cases2[0]);
但我的代码是:
public static int standingOvation(String cases){
char[] cases2 = cases.toCharArray();
return Integer.parseInt(cases2[0]);
}
如果我显然是在尝试将 cases
(作为 "11111"
传入)转换为整数,为什么我会收到错误消息?
尝试
return Integer.parseInt("" + cases2[0]);
通过添加到空字符串 ""
,您可以转换为字符串,这是 Integer.parseInt
的正确类型。
构造 cases2[0]
将从 cases2
字符串中选择第一个字符。
Integer.parseInt()
需要一个 String
参数,而不是 char
.
我收到错误消息 error: incompatible types: char cannot be converted to String
return Integer.parseInt(cases2[0]);
但我的代码是:
public static int standingOvation(String cases){
char[] cases2 = cases.toCharArray();
return Integer.parseInt(cases2[0]);
}
如果我显然是在尝试将 cases
(作为 "11111"
传入)转换为整数,为什么我会收到错误消息?
尝试
return Integer.parseInt("" + cases2[0]);
通过添加到空字符串 ""
,您可以转换为字符串,这是 Integer.parseInt
的正确类型。
构造 cases2[0]
将从 cases2
字符串中选择第一个字符。
Integer.parseInt()
需要一个 String
参数,而不是 char
.