Question

Can anyone let me know why this wordsearch method doesn't work - the returned value of count is 0 everytime I run it.

public int wordcount(){
    String spaceString = " ";
    int count = 0;
    for(int i = 0; i < this.getString().length(); i++){
        if (this.getString().substring(i).equals(spaceString)){
            count++;

        }
    }
    return count;
}

The value of getString = my search string. Much appreciated if anyone can help - I'm sure I'm prob doing something dumb. Dylan

Was it helpful?

Solution

Read the docs:

The substring begins with the character at the specified index and extends to the end of this string.

Your if condition is only true once, if the last character of the string is a space. Perhaps you wanted charAt? (And even this won't properly handle double spaces; splitting on whitespace might be a better option.)

OTHER TIPS

Because substring with only one argument returns the sub string starting from that index till the end of the string. So you're not comparing just one character. Instead of substring define spaceString as a char, and use charAt(i)

this.getString().substring(i) -> this returns a sub string from the index i to the end of the String

So for example if your string was Test the above would return Test, est, st and finally t

For what you're trying to do there are alternative methods, but you could simple replace

this.getString().substring(i)

with

spaceString.equals(this.getString().charAt(i))

An alternative way of doing what you're trying to do is:

this.getString().split(spaceString)

This would return an array of Strings - the original string broken up by spaces.

Read the documentation of the method you are using: http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int)

I.e. the count will be non zero only if you have a space on the end of your string

Using substring as you are will not work. If the value of getString() is "my search string" every iteration through the loop with have substring(i) return:

my search string
y search string
 search string
search string
earch string
arch string
rch string
ch string
h string
 string
string
tring
ring
ing
ng
g

Notice none of those equals " ".

Try using split.

public int countWords(String s){
    return s.split("\\s+").length;
}

Change

if (this.getString().substring(i).equals(spaceString))

to

if (this.getString().charAt(i) == ' ')

this.getString().substring(i) returns a string from the index of (i) to the end of the string. Example: for i=5, it will return "rown cow" from the string "the brown cow". This functionality isn't what you need.

If you pepper System.out.println() throughout your code (or use the debugger), you will see this. I think it would be better to use something like String.split() or charAt(i).

By the way, even if you fix your code by counting spaces, it will not return the correct value for these conditions: "my dog" (word count=2) and "cow" (word count=1). There is also a problem if there are more than one space between words. ALso, this will produce a word cound of three: " the cow ".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top