0

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.

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
user3236502
  • 45
  • 1
  • 3
  • 8

2 Answers2

1

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
}
Bitman
  • 1,996
  • 1
  • 15
  • 18
0

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.
}
xp500
  • 1,426
  • 7
  • 11