简体   繁体   中英

WordCount program doesn't count first word

Hi I'm new and confused when it comes to programming, I'm working on a program that count words in sentence string but it never counts the first word in the string, even when it's the only one. I know it's not an issue with case because I tested that already. Any help will be much appreciated! Here is my code

public class WordCount {
    public static boolean isWord (String w, int min) {
        int letters=0;
            for (int i=0; i<w.length(); i++) {
                char c=w.charAt(i);
                boolean l=Character.isLetter(c);
                if (l=true) {
                    letters++;
                }
                else {
                    c=' ';
                }
            }
        if (letters>min) {
            return true;
        }
        else {
            w=" ";
        }
        return false;
    }
    public static int countWords (String a, int minLength) {
        int count=0;
        for (int i=0; i<a.length(); i++) {
            if (a.charAt(i)==' ') {
                String b=a.substring(0, a.indexOf(' ')-1);
                if (isWord(b, minLength)==true) {
                    count++;
                }
            }   
        }
        return count;
    }
        public static void main (String[] args) {
        System.out.print("Enter a sentence: ");
        String sentence=IO.readString();
        System.out.print("Enter the minimum word length: ");
        int min=IO.readInt();
        if (min<0) {
            System.out.println("Bad input");
            return;
        }
        if (sentence.length()<min) {
            System.out.println("Bad input");
            return;
        }
        System.out.println("The word count is "+ countWords(sentence,min));
    }
}

The problem is that you are checking for a space as your delimiter for a word, so you are really counting spaces, not words. A single word like "foo" has no spaces, so it will return 0, whereas "foo bar" has a single space and would return 1. To test this try using an input of "foo bar " (with a trailing space) to get the correct count.

If you are happy with your current implementation and just want to "make it work" you can test to see if the trimmed input length is greater than zero, and if so append a space to the end before running it through your loop.

String sentence=IO.readString();
// make sure it is non-null
if (sentence!=null){
    // trim spaces from the beginning and end first
    sentence = sentence.trim();
    // if there are still characters in the string....
    if (sentence.length()>0){
       // add a space to the end so it will be properly counted.
       sentence += " ";
    }
}

An easier way to do this would be to split your String into an array using String.split() on a space, then counting the elements.

// your input
String sentence = "Hi there world!";

// an array containing ["Hi", "there", "world!"]
String[] words = sentence.split(" ");

// the number of elements == the number of words
int count = words.length;

System.out.println("There are " + count + " words.");

Will give you:

There are 3 words.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM