I want to check if the char at a certain index in a StringBuffer is equal to a char. I am getting an error, how do I compare them? Here's my code:
//I have an established StringBuffer called stringbuffer
char[] mychar = null;
for(int i = 0; i < stringbuffer.length(); ++i){
if(stringbuffer.charAt(i) != " "){
mychar[i] = stringbuffer.charAt(i);
}
}
The error I am getting is incompatible operand types char and String. Not sure why, because I am comparing a char of a StringBuffer, not a String or StringBuffer.
Values of char
type are surrounded with '
, Strings are surrounded with "
and you can't compare char
with String
with ==
in Java.
Try with
if(stringbuffer.charAt(i) != ' '){
or maybe cleaner (but this will check fore more than just simple ' '
)
if (! Character.isWhitespace(stringbuffer.charAt(i)) ) {