Jane
Jane

Reputation:

Counting the spaces in a string

I want to count the spaces in a string:

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

When I replace the if statement with if(temp.equals(" ")), I get a "cannot invoke(String) on the primitive type char.

I don't understand why this won't work.

Upvotes: 0

Views: 16285

Answers (4)

S N Prasad Rao
S N Prasad Rao

Reputation: 103

public class CountSpace {

public static void main(String[] args) {

    String word = "a b c";
    String data[];int k=0;
    data=word.split("");
    for(int i=0;i<data.length;i++){
        if(data[i].equals(" ")){
            k++;
        }

    }
    System.out.println(k);

}

}

Upvotes: 0

William Feirie
William Feirie

Reputation: 644

You can use commons-lang.jar to calculate this.

`public class Main {

public static void main(String[] args) {
    String word = "a b c";
    System.out.println("Spaces in string: " + StringUtils.countMatches(word," "));
}

}`

The source of "StringUtils.countMatches" is below:

public static int countMatches(String str, String sub) {
    if (isEmpty(str) || isEmpty(sub)) {
        return 0;
    }
    int count = 0;
    int idx = 0;
    while ((idx = str.indexOf(sub, idx)) != INDEX_NOT_FOUND) {
        count++;
        idx += sub.length();
    }
    return count;
}

Upvotes: 1

Barinder
Barinder

Reputation: 13

You can use the replace function for String to replace all the spaces(" ") with no spaces("") and get the difference between the lengths before and after calling the replace function. Go through this example:

class Test{

    public static void main(String args[]){

        String s1 = "a b c";
        int s1_length = s1.length();
        System.out.println(s1_length); // 5

        String s2 = s1.replace(" ",""); 
        int s2_length = s2.length();
        System.out.println(s2_length); // 3

        System.out.println("No of spaces = " + (s1_length-s2_length)); // No of spaces = 2
    }
}

Upvotes: 1

paulolc
paulolc

Reputation: 86

It won't work because you are calling a method of Class String (equals()) on a value which is of primitive type 'char'. You are trying to compare a 'char' with a 'String'.

You must compare between 'char's and since it's a primitive value you need to use '==' boolean compare operator like:

public class SongApp {

    public static void main(String[] args) {

      String word = "a b c";
      int i = 0,
      spaceCount = 0;

      while( i < word.length() ){
        if( word.charAt(i) == ' ' ) {
            spaceCount++;
        }
        i++;
      }

      System.out.println("Spaces in string: "+spaceCount);
    }
}

Upvotes: 7

Related Questions