Kodekraftt
/Blog
/Count Words In A Sentence In Java Definition Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Count Words in a Sentence in Java — Definition, Examples and Program

Learn how to count words in a sentence using Java. This blog explains the logic with examples, a clean Java program, and practice challenges for beginners.

Count Words in a Sentence in Java — Definition, Examples and Program

Counting words in a sentence is a common string problem. It helps in text analysis, document processing, and input validation.

In this blog, you will learn:

  • What a word is
  • How to count words correctly
  • Step-by-step examples
  • A Java program
  • Practice challenges

What Is a Word?

A word is a group of characters separated by spaces. Multiple spaces between words should not be counted as extra words.

How the Logic Works

To count words in a sentence:

  1. Ignore leading and trailing spaces.
  2. Count transitions from space to non-space characters.
  3. Each transition indicates the start of a new word.

Example 1

Input: "Java is fun"

  • Words: Java, is, fun

Total Words: 3

Example 2

Input: " Learn Java Programming "

  • Extra spaces are ignored
  • Words: Learn, Java, Programming

Total Words: 3

Java Program to Count Words


import java.util.Scanner;

public class CountWords {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a sentence: ");
        String sentence = sc.nextLine();

        int wordCount = 0;
        boolean isWord = false;

        for (int i = 0; i < sentence.length(); i++) {
            char ch = sentence.charAt(i);

            if (ch != ' ' && !isWord) {
                wordCount++;
                isWord = true;
            } else if (ch == ' ') {
                isWord = false;
            }
        }

        System.out.println("Number of words: " + wordCount);
    }
}

Sample Output


Enter a sentence: Java programming is powerful
Number of words: 4

Practice Challenges

  1. Count words without using any extra boolean variable.
  2. Count words using split() and compare both approaches.
  3. Find the longest word in a sentence.

Counting words accurately requires careful handling of spaces. This problem builds strong fundamentals in string traversal and condition handling.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.