I am trying to compare char to a String but getting an error. Error
error: incompatible types: unexpected return value
code:
The type of main()
is void
(public static
void main(String args[]) {
)
and therefore you cannot return a value from it.
The actual error I get when I run this is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Void methods cannot return a value
The main method can not return anything, it has a void
type.
You can write a separate function that returns that value, assign the value to a variable, or just print the value.
public boolean strChar(String s, String p){
return p.equals(new String(new char[]{c}));
}
or
System.out.println(p.equals(new String(new char[]{c})));
or
boolean equals = p.equals(new String(new char[]{c}));
Characters are not objects and can / should be compared using the ==
operator. There is no need to build a string each time. Also the main
method can't return any value, it is void
.