Here's what I've got:
private static int countNumChars(String s) {
for(char c : s.toCharArray()){
if (Equals(c," "))
}
}
But that code says it cannot find Symbol for that method. I remember Java having a comparer like this... Any suggestions?
To compare
Strings
you have to use the equals keyword.Since
char
is a primitive type, you can just writec == ' '
.You only need to call
equals()
for reference types likeString
orCharacter
.You can try:
Or:
char
is a primitive data type, so it can be compared with==
.Also, by using double quotes you create
String
constant (" "
), while with single quotes it's achar
constant (' '
).In this case, you are thinking of the String comparing function
"String".equals("some_text")
. Chars do not need to use this function. Instead a standard==
comparison operator will suffice.My suggestion would be: