How to count number of sentences in a text file in Java

If you are wondering how many sentences does a .txt file contains? You will get your question answered here! In this tutorial, we are going to study how many sentences are present in a text file in Java. Using a simple Java program you will get your solution.

In this tutorial, you will get to learn-

  • How to open a file in Read mode in Java.
  • Use of regular expression in Java.

Let’s get started!
Basically, this whole idea can be divided into the following steps-

  1. Open the file in Read mode.
  2. Read the content of the file line by line.
  3. Iterate till the end of the file.
  4. Using Regular Expression look for the pattern and find a match(sentences are those which end with ‘.’, ‘?’ and ‘!’ and a space).
  5. Finally, count sentences present in the file.

Count the number of sentences in a text file in Java

Let’s start coding these steps!

  • Import io package as we are handling with the files and util.regex package for the regular expression.
import java.io.*;		
import java.util.regex.*;
  • Create an object of the BufferedReader as it is a way to read the content of the file. But we can’t communicate directly with the file, we need some reader object. Generally, FileReader is used to communicate with the file, therefore create another object of FileReader within BufferedReader.
BufferedReader br = new BufferedReader(new FileReader("filename.txt"));
  • Read the content of the file line by line and store it in a string variable.
  • Using the concept of regular expression search for the pattern for e.g. full stop followed by space(. ), a question mark followed by space(? ), and exclamation mark followed by space(! ). If the match is found increment the counter.
int stmt=0;
String s;
while((s=br.readLine())!=null){	
     Pattern p = Pattern.compile("([.?!][ ])");	
     Matcher m = p.matcher(s);
     while(m.find()){
         stmt++;
     }
}
  • Finally, print the counter value which will give the number of sentences.
System.out.println("number of sentences: "+stmt);

Count number of sentences in Java

Below is the give code for our task:

import java.io.*;		//import io packages
import java.util.regex.*;	//import package for regular expression
public class count_sentences{
  public static void main(String[] args)throws IOException{
    //to open the file in read mode
        	BufferedReader br = new BufferedReader(new FileReader("filename.txt"));		

    int stmt=0;
    String s;
    //read the content line by line and iterate till the end of the file
    while((s=br.readLine())!=null){	
      Pattern p = Pattern.compile("([.?!][ ])");	//match pattern using regular expression
  			Matcher m = p.matcher(s);
      while(m.find()){
        stmt++;	
      }
    }
    
    System.out.println("number of sentences: "+stmt);				
    br.close(); 		//close the file
  }
}

Content of the file

What is the full form of W.H.O.? This is statement. 
This seems amazing! This is another statement.

Output

number of sentences: 4

This is how we can find the number of sentences. I hope you find this tutorial useful.

One response to “How to count number of sentences in a text file in Java”

  1. Shivan Mistry says:

    Greatly done , every detailed information,
    Superb

Leave a Reply

Your email address will not be published. Required fields are marked *