119

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?

Jonik
  • 80,077
  • 70
  • 264
  • 372

11 Answers11

245
if (c == ' ')

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 a char constant (' ').

Nikita Rybak
  • 67,365
  • 22
  • 157
  • 181
91

The code you needs depends on what you mean by "an empty space".

  • If you mean the ASCII / Latin-1 / Unicode space character (0x20) aka SP, then:

    if (ch == ' ') {
        // ...
    }
    
  • If you mean any of the traditional ASCII whitespace characters (SP, HT, VT, CR, NL), then:

    if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '\x0b') {
        // ...
    }
    
  • If you mean any Unicode whitespace character, then:

    if (Character.isWhitespace(ch)) {
        // ...
    }
    

Note that there are Unicode whitespace includes additional ASCII control codes, and some other Unicode characters in higher code planes; see the javadoc for Character.isWhitespace(char).


What you wrote was this:

    if (Equals(ch, " ")) {
        // ...
    }

This is wrong on a number of levels. Firstly, the way that the Java compiler tries to interpret that is as a call to a method with a signature of boolean Equals(char, String).

  • This is wrong because no method exists, as the compiler reported in the error message.
  • Equals wouldn't normally be the name of a method anyway. The Java convention is that method names start with a lower case letter.
  • Your code (as written) was trying to compare a character and a String, but char and String are not comparable and cannot be cast to a common base type.

There is such a thing as a Comparator in Java, but it is an interface not a method, and it is declared like this:

    public interface Comparator<T> {
        public int compare(T v1, T v2);
    }

In other words, the method name is compare (not Equals), it returns an integer (not a boolean), and it compares two values that can be promoted to the type given by the type parameter.


Someone (in a deleted Answer!) said they tried this:

    if (c == " ")

That fails for two reasons:

  • " " is a String literal and not a character literal, and Java does not allow direct comparison of String and char values.

  • You should NEVER compare Strings or String literals using ==. The == operator on a reference type compares object identity, not object value. In the case of String it is common to have different objects with different identity and the same value. An == test will often give the wrong answer ... from the perspective of what you are trying to do here.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
20

You could use

Character.isWhitespace(c)

or any of the other available methods in the Character class.

  if (c == ' ')

also works.

Corv1nus
  • 4,523
  • 1
  • 26
  • 37
  • Well it ultimately depends on if he is checking for spaces only. I mean if it contains a tab, you would get a false positive. –  Dec 22 '10 at 14:30
  • Updated. He can do what he needs directly from Character, although it is overkill. – Corv1nus Dec 22 '10 at 14:33
18

Since char is a primitive type, you can just write c == ' '.
You only need to call equals() for reference types like String or Character.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
12

My suggestion would be:

if (c == ' ')
MartinStettner
  • 28,719
  • 15
  • 79
  • 106
11

To compare character you use the == operator:

if (c == ' ')
codaddict
  • 445,704
  • 82
  • 492
  • 529
7

Character.isSpaceChar(c) || Character.isWhitespace(c) worked for me.

vbevans94
  • 1,500
  • 1
  • 16
  • 15
4

You can try:

if(Character.isSpaceChar(ch))
{
    // Do something...
}

Or:

if((int) ch) == 32)
{
    // Do something...
}
A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
4

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.

private static int countNumChars(String s) {
    for(char c : s.toCharArray()){
        if (c == ' ') // your resulting outcome
    }
}
3

At first glance, your code will not compile. Since the nested if statement doesn't have any braces, it will consider the next line the code that it should execute. Also, you are comparing a char against a String, " ". Try comparing the values as chars instead. I think the correct syntax would be:

if(c == ' '){
   //do something here
}

But then again, I am not familiar with the "Equal" class

bakoyaro
  • 2,550
  • 3
  • 36
  • 63
-5

To compare Strings you have to use the equals keyword.

if(c.equals(""))
{
}
Sergiu Paraschiv
  • 9,929
  • 5
  • 36
  • 47
  • 1) This Question is about comparing characters, not strings. 2) There is no `equals` keyword in Java. It is an identifier. 3) Since `c` is a `char` (primitive type!!), you can't call `c.equals(...)`. 4) Even if you could, there is no `char` value that maps to `""`. The latter means a String with "no characters in it". That cannot match a character. – Stephen C Jul 05 '17 at 22:47
  • The key to answering this Question is coming up with a meaning for "empty space" that makes some kind of sense in the context. – Stephen C Jul 06 '17 at 03:56