Java Program to Count Words in a Given Text

This program counts the number of words in a given text. It uses Java’s built-in string manipulation methods to achieve this. Below is the complete Java program along with a detailed explanation of its structure and functionality.

Program Structure and Explanation

The program consists of a single class WordCounter with the following components:

  • main method: This is the entry point of the program. It calls the countWords method with a sample text.
  • countWords method: This method takes a string as input and returns the number of words in it. It uses the split method of the String class to split the text into words based on spaces and returns the length of the resulting array.

Java Code

/**
 * WordCounter.java
 *
 * This program counts the number of words in a given text.
 */
public class WordCounter {

    /**
     * The main method is the entry point of the program.
     * @param args Command line arguments
     */
    public static void main(String[] args) {
        String text = "This is a sample text to count the number of words.";
        int wordCount = countWords(text);
        System.out.println("The number of words in the given text is: " + wordCount);
    }

    /**
     * This method counts the number of words in the given text.
     * @param text The input text
     * @return The number of words in the text
     */
    public static int countWords(String text) {
        // Split the text by spaces to get individual words
        String[] words = text.split("\\s+");
        // Return the number of words
        return words.length;
    }
}

Explanation of Key Components

main Method

The main method is where the program starts execution. It contains a sample text and calls the countWords method to count the number of words in this text. The result is then printed to the console.

countWords Method

The countWords method is a static method that takes a string as input. It uses the split method of the String class with a regular expression "\\s+" to split the text into words. The regular expression "\\s+" matches one or more whitespace characters. The method then returns the length of the resulting array, which represents the number of words in the input text.

Sample Output

When the program is executed with the sample text “This is a sample text to count the number of words.”, the output will be:

The number of words in the given text is: 10

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

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

error

Enjoy this blog? Please spread the word :)