0

I can't seem to figure out why this doesn't work, but I may have just missed some simple logic. The method doesn't seem to find the last word when there isn't a space after it, so i'm guessing something is wrong with i == itself.length() -1 , but it seems to me that it would return true; you're on the last character and it isn't a whitespace.

public void numWords()
{
    int numWords = 0;
    for (int i = 1; i <= itself.length()-1; i ++)
    {
        if (( i == (itself.length() - 1) || itself.charAt (i) <= ' ') && itself.charAt(i-1) > ' ')
            numWords ++;
    }
    System.out.println(numWords);
}

itself is the string. I am comparing the characters the way I am because that's how it is shown in the book, but please let me know if there are better ways.

Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
user2034276
  • 49
  • 1
  • 8
  • http://stackoverflow.com/questions/8924599/how-to-count-the-exact-number-of-words-in-a-string-that-has-empty-spaces-between – HpTerm Jan 28 '14 at 18:20
  • Can't you just split on white space? http://stackoverflow.com/questions/225337/how-do-i-split-a-string-with-any-whitespace-chars-as-delimiters – plalx Jan 28 '14 at 18:22
  • Thanks for the link, I didn't see that with my brief search. – user2034276 Jan 28 '14 at 18:25

4 Answers4

1

Naïve approach: treat everything that has a space following it as a word. With that, simply count the number of elements as the result of a String#split operation.

public int numWords(String sentence) {
    if(null != sentence) {
        return sentence.split("\\s").length;
    } else {
        return 0;
    }
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
0

Try,

int numWords = (itself==null) ? 0 : itself.split("\\s+").length;
Rakesh KR
  • 6,357
  • 5
  • 40
  • 55
0

So basically what it seems you're trying to do it to count all chunks of whitespace in a string. I'll fix up your code and use my head compiler to help you out with the problems you're experiencing.

public void numWords()
{
    int numWords = 0;
    // Don't check the last character as it doesn't matter if it's ' '
    for (int i = 1; i < itself.length() - 1; i++)
    {
        // If the char is space and the next one isn't, count a new word
        if (itself.charAt(i) == ' ' && itself.charAt(i - 1) != ' ') {
            numWords++;
        }
    }
    System.out.println(numWords);
}

This is a very naive algorithm and fails in a few cases, if the string ends in multiple spaces for example 'hello world ', it would count 3 words.

Note that if I was going to implement such a method I would go with a regex approach similar to Makoto's answer in order to simplify the code.

Daniel Imms
  • 47,944
  • 19
  • 150
  • 166
0

The following code fragment does job better:

if(sentence == null) {
    return 0;
}
sentence = sentence.trim();
if ("".equals(sentence)) {
    return 0;
}
return sentence.split("\\s+").length;
  • The regex \\s+ works correctly in case of several spaces. trim()
  • removes trailng and leading spaces Additional empty line check
  • prevents result 1 for empty string.
AlexR
  • 114,158
  • 16
  • 130
  • 208