0

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?

2
  • -.- I'm not saying the word is an array. I'm saying i dont want to use a string array. Commented Nov 26, 2013 at 7:06
  • Frankly, your reason for not using arrays sounds like: I can't go to the cinema, because I have a white car, and I can't hardwire my white car to going to the cinema. It doesn't make any sense. Using an array doesn't force you to hardwire anything. Commented Nov 26, 2013 at 7:12

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm failing to see how using two different scanners will help. If the first scanner can't delimit using my code what difference will a different scanner make?
1

Maybe, this is what you are looking for.

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


}

3 Comments

The OP said that he doesn't want to use arrays, but your code implicitly use arrays. But nevertheless, nice strategy to avoid the looping. +1 for that.. :)
He mentioned he did not want to use arrays as it complicates his code. I have used something that does it implicitly with arrays and I honestly dont think, his code can get any simpler than this :)
Look at Dubinsky code below. I think that's what I meant but didn't know how to do. thanks for your help all the same!
0

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

2 Comments

hey thanks! this worked the best but it's off by 1 word each line. how do i fix that?
i think you mean it displays the count for each line only. look at my edit. it will give you the entire count of words of the file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.