0
votes

this statement shows this error: method contains in class String cannot be applied to given types;

boolean m=str[j].contains(a); 

why this happens and what is the remedy.It would be great if someone helps.. thank you..

3
what is the datatype of str[j] - jmj
What is the datatype of a? - rgettman
@JigarJoshi It's of type String. The more important question is what is the type of 'a'. - Smith_61
Obviously a is not instanceof CharSequence. - Eran
a is of Character datatype...what is the difference between the two ? - ashwin

3 Answers

3
votes

Since the method contains() need to receive a CharSequence, probably your variable a is not an instance of CharSequence.

So, do a validation:

if(a instanceof CharSequence ){
    boolean m=str[0].contains(a); 
}

---- Edit Try this:

CharSequence a = 'a'.toString();
boolean m = str[0].contains(a);
0
votes

When you say str[j], you are subscripting it, so str[j] is a char. And contains() method cannot be applied on char. What you want to do is probably this :

String str = "StackOverflow";
boolean isOPresent = str.contains('O');
System.out.println(isOPresent); // will print true
0
votes

Without the code that surrounds this statement it will be difficult to tell exactly what's wrong. But from that one line, my guess is that either the variable a is not of type char or str is not a string but rather a char as well. Post more code and maybe we can help more thoroughly

UPDATE: The variable that is sent in to the 'contains' function must be of type CharSequence according to the Java Docs.

http://docs.oracle.com/javase/7/docs/api/java/lang/String.html

http://docs.oracle.com/javase/7/docs/api/java/lang/CharSequence.html