[英]Error: Incompatible String Types: Unexpected return value - This is the error of my code when I return a string
[英]error: incompatible types: unexpected return value Char compare to String
我正在尝试将char与String进行比较,但出现错误。 错误
error: incompatible types: unexpected return value
码:
public class MyClass {
public static void main(String args[]) {
String s = Integer.toBinaryString(2432);
String p = "0";
for (char c : s.toCharArray()) {
return p.equals(new String(new char[]{c}));
}
}
}
main()
的类型为void
( public static
void main(String args[]) {
),因此您不能从中返回值。
运行此命令时得到的实际错误是:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Void methods cannot return a value
字符不是对象,可以/应该使用==
运算符进行比较。 无需每次都构建一个字符串。 同样main
方法不能返回任何值,它是void
。
main方法不能返回任何内容,它具有void
类型。 您可以编写一个单独的函数来返回该值,将该值分配给变量或仅打印该值。
public boolean strChar(String s, String p){
return p.equals(new String(new char[]{c}));
}
要么
System.out.println(p.equals(new String(new char[]{c})));
要么
boolean equals = p.equals(new String(new char[]{c}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.