title: FileStatistics -- trouble counting the number of words in a file
body: <p>In my course, we are tasked with determining three key statistics about a file that is passed via the console input: 1) number of characters, 2) number of lines, 3) number of words. Before closing this question as a duplicate, please read on to see what unique problem I'm encountering. Thank you :)</p>
  
  <p>I originally wrote a solution with three separate methods and three separate Scanner variables, but I realized that for larger files, this solution would be very inefficient. Instead, I decided to write up a solution that only runs through the file a single time and calculates all three statistics in one go. Here is what I have so far:</p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  
  
  public class FileStatistics
  {   
  
      // Note: uncomment (A) and (B) below to test execution time
  
      public static void main( String [] args ) throws IOException
      {
  
          /* (A)
          long startTime = System.currentTimeMillis();
          */
  
          File file = new File(args[0]);
          Scanner input = new Scanner(file);
          int numChars = 0, numWords = 0, numLines = 0;
  
  
  
          /* Calculations */
  
          while( input.hasNextLine() )
          {
              String currentLine = input.nextLine();
              numLines++;
              numChars+= currentLine.length();
  
              String [] words = currentLine.split(" ");
              numWords += words.length;               
          }
          input.close();
  
  
  
          /* Results */
          System.out.println( "File " + file.getName() + " has ");
          System.out.println( numChars + " characters");
          System.out.println( numWords + " words");
          System.out.println( numLines + " lines");
  
  
          /* (B) 
          long endTime = System.currentTimeMillis();
          System.out.println("Execution took: " + (endTime-startTime)/1000.0 + " seconds");
          */
  
      }
  
  
  }
  </code></pre>
  
  <p><br></p>
  
  <p>I've been comparing the results of my program to Microsoft Word's own file statistics by simply copy/pasting the contents of whatever file I'm using into Word. The number of characters and number of lines are calculated correctly.</p>
  
  <p>However, my program does not properly count the number of words. I decided to include a test statement in there to print out the contents of the array <code>words</code>, and it seems that certain "spatial formatting" (like tabs from a Java source code file) are being treated as individual elements in the split array. I tried doing <code>currentLine.replace("\t", "")</code> before invoking the split method  to remove those tabs, but this didn't change a thing. </p>
  
  <p>Could someone please offer some advice or hints as to what I'm doing wrong?</p>