文字列内のスペースをカウントしたい:
public class SongApp {
public static void main(String[] args) {
String word = "a b c";
int i =0,spaceCount=0;
while(i<word.length()){
char temp = word.charAt(i);
System.out.println(temp);
if(" ".equals(temp)){
spaceCount++;
}
i++;
}
System.out.println("Spaces in string: "+spaceCount);
}
}
if ステートメントを に置き換えるとif(temp.equals(" "))
、"cannot invoke(String) on the Primitive type char.
これがうまくいかない理由がわかりません。