Word, Character And Line Count Program In Java

Introduction

  • In this blog, I am going to explain the program for word, character and line count in Java.

Software Requirements

  • Java, Notepad.

Program

  1. import java.io.*;  
  2.   
  3. public class  WordCount{  
  4.     private static void linecount(String fName, BufferedReader in) throws IOException{  
  5.     long numChar = 0;  
  6.     long numLine=0;  
  7.     long numWords = 0;  
  8.     String line;  
  9.         do{  
  10.             line = in.readLine();  
  11.             if (line != null){  
  12.                 numChar += line.length();  
  13.                 numWords += wordcount(line);  
  14.                 numLine++;  
  15.             }  
  16.         }while(line != null);  
  17.         System.out.println("File Name: " + fName);  
  18.         System.out.println("Number of characters: " + numChar);  
  19.         System.out.println("Number of words: " + numWords);  
  20.         System.out.println("Number of Lines: " + numLine);  
  21.     }  
  22.     private static void linecount(String fileName){  
  23.         BufferedReader in = null;  
  24.         try{  
  25.             FileReader fileReader = new FileReader(fileName);  
  26.             in = new BufferedReader(fileReader);  
  27.             linecount(fileName,in);  
  28.         }  
  29.         catch(IOException e){  
  30.             e.printStackTrace();  
  31.         }  
  32.     }  
  33.     private static long wordcount(String line){  
  34.         long numWords = 0;  
  35.         int index = 0;  
  36.         boolean prevWhiteSpace = true;  
  37.         while(index < line.length()){  
  38.             char c = line.charAt(index++);  
  39.             boolean currWhiteSpace = Character.isWhitespace(c);  
  40.             if(prevWhiteSpace && !currWhiteSpace){  
  41.                 numWords++;  
  42.             }  
  43.             prevWhiteSpace = currWhiteSpace;  
  44.         }  
  45.         return numWords;  
  46.     }  
  47.     public static void main(String[] args){  
  48.         long numChar = 0;  
  49.         long numLine=0;  
  50.         String line;  
  51.         try{  
  52.             if (args.length == 0)  
  53.             {  
  54.                 BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  
  55.                 line = in.readLine();  
  56.                 numChar = line.length();  
  57.                 if (numChar != 0){  
  58.                     numLine=1;  
  59.                 }  
  60.                 System.out.println("Number of characters: " + numChar);  
  61.                 System.out.println("Number of words: " + wordcount(line));  
  62.                 System.out.println("Number of lines: " + numLine);  
  63.             }else{  
  64.                 for(int i = 0; i < args.length; i++){  
  65.                     linecount(args[i]);  
  66.                 }  
  67.             }  
  68.         }  
  69.         catch(IOException e){  
  70.             e.printStackTrace();  
  71.         }  
  72.     }  
  73. }  

Output

Output

Ebook Download
View all
Learn
View all