Domanda

I need to count the words in a String. For many of you that seems pretty simple but from what I've read in similar questions people are saying to use arrays but I'd rather not. It complicates my program more than it helps as my string is coming from an input file and the program cannot be hardwired to a specific file.

I have this so far:

while(input.hasNext())
    {
        String sentences = input.nextLine();
       int countWords;
       char c = " ";
       for (countWords = 0; countWords < sentences.length(); countWords++)
       {
            if (input.hasNext(c))
                countWords++;
       }

       System.out.println(sentences);
       System.out.println(countWords);
    }

The problem is that what I have here ends up counting the amount of characters in the string. I thought it would count char c as a delimiter. I've also tried using String c instead with input.hasNext but the compiler tells me:

Program04.java:39: incompatible types
found   : java.lang.String[]
required: java.lang.String
       String token = sentences.split(delim);

I've since deleted the .split method from the program. How do I delimit (is that the right word?) without using a String array with a scanned in file?

È stato utile?

Soluzione

Don't use the Scanner (input) for more than one thing. You're using it to read lines from a file, and also trying to use it to count words in those lines. Use a second Scanner to process the line itself, or use a different method.

The problem is that the scanner consumes its buffer as it reads it. input.nextLine() returns sentences, but after that it no longer has them. Calling input.hasNext() on it gives you information about the characters after sentences.

The simplest way to count the words in sentences is to do:

int wordCount = sentences.split(" ").length;

Using Scanner, you can do:

Scanner scanner = new Scanner(sentences);
while(scanner.hasNext())
{
     scanner.next();
     wordCount++;
}

Or use a for loop for best performance (as mentioned by BlackPanther).

Another tip I'd give you is how to better name your variables. countWords should be wordCount. "Count words" is a command, a verb, while a variable should be a noun. sentences should simply be line, unless you know both that the line is composed of sentences and that this fact is relevant to the rest of your code.

Altri suggerimenti

Maybe, this is what you are looking for.

 while(input.hasNext())
{
   String sentences = input.nextLine();
   System.out.println ("count : " + line.split (" ").length);


}

what you are trying to achieve is not quite clear. but if you are trying to count the number of words in your text file then try this

int countWords = 0;

while(input.hasNext())
{
   String sentences = input.nextLine();
   for(int i = 0; i< sentences.length()-1;i++ ) {
       if(sentences.charAt(i) ==  " ") {
          countWords++;
       }
   }
}
System.out.println(countWords);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top