Question

I am making a program that makes an user choose a file then the program reads from the file. Now I've been told to make the program using bufferedreader and string tokenizer. So far I got program opening the file and counting the number of lines. But the number of words is not so easy.

This is my code so far:

int getWords() throws IOException
{
   int count = 0;
   BufferedReader BF = new BufferedReader(new FileReader(f));
   try  {
      StringTokenizer words = new StringTokenizer(BF.readLine()); 
      while(words.hasMoreTokens())
      { 
         count++;
         words.nextToken(); 
      }
      BF.close();
   }  catch(FileNotFoundException e)  {
   }    
   return count;
}

Buffered reader can only read a line at a time but I don't know how to make it read more lines.

Was it helpful?

Solution 2

As you said, buffered reader will read one line at a time. So you have to read lines until there are no more lines. readLine() returns null when the end of file is reached.

So do something like this

int getWords() throws IOException {
  int count = 0;
  BufferedReader BF = new BufferedReader(new FileReader(f));
  String line;
  try {
    while ((line = BF.readLine()) != null) {
      StringTokenizer words = new StringTokenizer(line); 
      while(words.hasMoreTokens()) { 
        count++;
        words.nextToken(); 
      }    
    }
    return count;
  } catch(FileNotFoundException e)  {
  } finally {
    BF.close();
  }
  // Either rethrow the exception or return an error code like -1.
}

OTHER TIPS

to count words you can use countTokens() instead of loop

to read all lines use

String line = null;
while(null != (line = BF.readLine())) {
StringTokenizer words = new StringTokenizer(line); 
   words.countTokens();//use this value as number of words in line
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top