How to check if a char is equal to an empty space?

JavaCharWhitespace

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

You could use

Character.isWhitespace(c)

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

  if (c == ' ')

also works.

Solution 4 - Java

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

Solution 5 - Java

My suggestion would be:

if (c == ' ')

Solution 6 - Java

To compare character you use the == operator:

if (c == ' ')

Solution 7 - Java

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

Solution 8 - Java

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
    }
}

Solution 9 - Java

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

Solution 10 - Java

You can try:

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

Or:

if((int) ch) == 32)
{
    // Do something...
}

Solution 11 - Java

To compare Strings you have to use the equals keyword.

if(c.equals(""))
{
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondeleteView Question on Stackoverflow
Solution 1 - JavaNikita RybakView Answer on Stackoverflow
Solution 2 - JavaStephen CView Answer on Stackoverflow
Solution 3 - JavaCorv1nusView Answer on Stackoverflow
Solution 4 - JavaSLaksView Answer on Stackoverflow
Solution 5 - JavaMartinStettnerView Answer on Stackoverflow
Solution 6 - JavacodaddictView Answer on Stackoverflow
Solution 7 - Javavbevans94View Answer on Stackoverflow
Solution 8 - Javauser372743View Answer on Stackoverflow
Solution 9 - JavabakoyaroView Answer on Stackoverflow
Solution 10 - JavaA-SharabianiView Answer on Stackoverflow
Solution 11 - JavaAdnan B.View Answer on Stackoverflow