Home Java Beginners Java Word Count - Word Count Example in Java

 
 

Share on Google+Share on Google+

Java Word Count - Word Count Example in Java

Advertisement
This example illustrates how to count the number of lines, number of words and number of characters in the specified file.

Java Word Count - Word Count Example in Java

     

This example illustrates how to count the number of lines, number of words and number of characters in the specified file. Program takes the file name as parameter and it counts the number of words and lines present in the file. Parameter is optional and if you simply run the program without mentioning the file name then you will have to input some strings and program will count the number of characters and number of words for your given strings. This topic is related to the I/O (input/output) of java.io package.

In this example we are using FileReader class of java.io package. The File class is an abstract representation of file and directory pathnames. 

Explanation

This program counts the number of lines, number of words and number of characters in the specified file. We will be declaring two functions called wordcount and linecount in the program. The function linecount has been overloaded according to the passing argument. If you input contents through the file then linecount function will be called (If specified file exists) otherwise main function counts the number of characters and number of lines (always, the number of line will be only 1 in this condition) itself but for the counting of the number of words by using the wordcount function.

wordcount(String line)

The function wordcount(String line) takes either the content of the specified file or arguments passed with the run command for a java program as parameter 'String line'. The wordcount() function is using arrayname.charAt(index) to find position of space in the string.  A counter variable 'numWords' is used to count the number of words.

linecount(String fileName);

The function linecount(String fileName) takes the specified file name as a string parameter and create a instance for the FileReader class to buffering then the file or string and it is passed to the function linecount(String fName, BufferedReader in).

linecount(String fileName, BufferedReader);

The function linecount(String fName, BufferedReader in) takes the specified file name and created instance in for the BufferedReader class by calling function linecount(String fileName) and assign the content of the buffer in a string variable line. And then the function linecount(String fileName, BufferedReader) counts and print the number of characters, number of lines. To count the number of words call the wordcount(String line) function.

Code of the Program : 

import java.io.*;

public class  WordCount{
  private static void linecount(String fName, BufferedReader in) 
  throws 
IOException{
  long numChar = 0;
  long numLine=0;
  long numWords = 0;
  String line;
 
 do{
  line = in.readLine();
  if (line != null){
  numChar += line.length();
 
 numWords += wordcount(line);
  numLine++;
  }
  }
while(line != null);
  System.out.println("
File Name: " + fName);
  System.out.println("
Number of characters: " + numChar);
  System.out.println("Number of words: " + numWords);
  System.out.println("Number of Lines: " + numLine);
  }

  private static void linecount(String fileName){
  BufferedReader in =
 null;
  
try{
  FileReader fileReader = 
new FileReader(fileName);
  in = 
new BufferedReader(fileReader);
  linecount(fileName,in);
  }
  
catch(IOException e){
  e.printStackTrace();
  }
  }
  private static long wordcount(String line){
  
long numWords = 0;
  int index = 0;
  boolean prevWhiteSpace = true;
  while(index < line.length()){
 
 char c = line.charAt(index++);
  boolean currWhiteSpace = Character.isWhitespace(c);
 
 if(prevWhiteSpace && !currWhiteSpace){
  numWords++;
  }
  prevWhiteSpace = currWhiteSpace;
  }
 
 return numWords;
  }
  public static void main(String[] args){
  
  long numChar = 0;
 
 long numLine=0;
  String line;
  try{
  if (args.length == 0)
  {
  BufferedReader in = 
 
new BufferedReader(new InputStreamReader(System.in));
  line = in.readLine();
  numChar = line.length();
 
 if (numChar != 0){
  numLine=1;
  }
  System.out.println(
"Number of characters: " + numChar);
  System.out.println("Number of words: " + wordcount(line));
  System.out.println("Number of lines: " + numLine);
  }else{
  for(int i = 0; i < args.length; i++){
  linecount(args[i]);
  }
  }
  }
  catch(IOException e){
  e.printStackTrace();
  }
  }
}

Download Word count Example

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 11, 2008

Related Tutorials

Discuss: Java Word Count - Word Count Example in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:5
Shubham
April 21, 2011
Help

Could someone help me understand the word count part of the code? thanks
Luis A.
June 2, 2011
GRACIAS (Thanks)

Your example is very good thanks for help me with my homework.
noman nazir
March 5, 2013
programming

good web site 4 prgramming....
June
April 28, 2012
input

what is the input in the command line?
Taban cosmos
May 28, 2012
File count

I have found this SAMPLE CODES EXAMPLE AND EXPLANATION very help full and would request you guys to do something on seat reservation. Creating a reservation for a ship company and I would like to understand how object oriented programing work. THANK YOU GUYS...
DMCA.com