Table of contents
1.
Introduction
1.1.
Example
1.2.
Output
2.
Word Character Counter in Java
2.1.
AWT/Swing
3.
Explanation
3.1.
Code
3.2.
Output
4.
Frequently Asked Questions
4.1.
In Java, how do you count characters in a string array?
4.2.
Why are Swings used in Java?
4.3.
What is a combobox in Java?
4.4.
What is a JButton in Java?
4.5.
Is Swing considered a framework?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Word Character Counter in Java

Author Kanak Rana
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A single object used to represent text, numbers, symbols, or special characters is referred to as a character. For example, the letter 'a' is considered a single character, whereas the word "Java" is regarded as a four-character string.

Word Character Counter in Java

The char keyword is commonly used in Java to represent characters. Java's data type is char, and it is used to store a single character. The char value should be surrounded by single quotes. In Java, a string is defined as zero or more characters. The string value should be surrounded by double-quotes. The code below demonstrates how to declare characters and strings in Java.

Example

class CharCount{
    public static void main(String[] args) {
        String str = "This is Coding Ninjas hello!!";
        int len = str.length();
        System.out.println("Length of the String: " + len);
    }
}
You can also try this code with Online Java Compiler
Run Code

Output

Output

There are many ways to count the number of characters in Java. But here, we focus on using AWT(Abstract Window Toolkit)/String.

Word Character Counter in Java

We can create a Word Character Counter in Java using string, AWT/Swing, with event handling with the help of ActionEvent Class.

AWT/Swing

The Java AWT API(Application Programming interface) is used to create graphical user interface (GUI) applications. Java Swing is a Java Foundation Class that is used to create a variety of applications.

JAVA AWT JAVA Swing
The Java AWT components are heavily weighted. Java Swing's components are lightweight.
When compared to Swing, Java AWT has less functionality. When compared to AWT, Java Swing provides more functionality.
MVC is not supported. MVC is supported.
Components are platform dependent. Components are platform Independent.

Explanation

In this program we have use the length() function  to count the characters in a string. Here, when, the length() function with the given string enter by the user, it will return the count as the output for both word and character.

Explanation

Code

Word Character Counter in Java program: In this program, we will find out the word and the character count in the lang-java, where the user inputs the text or a paragraph.

import java.awt.event.*;
import javax.swing.*;

public class CharCount extends JFrame implements ActionListener
{
	JTextArea t;
	JButton button1,button2;
	CharCount()
	{
		super("Word Character Counter in Java- Coding Ninjas");
		t=new JTextArea();
		t.setBounds(60,60,350,250);
		
		button1=new JButton("Word");
		button1.setBounds(100,350,140,40);
		
		button2=new JButton("Character");
		button2.setBounds(230,350,140,40);
		
		button1.addActionListener(this);
		button2.addActionListener(this);
		add(button1);add(button2);add(t);
		setSize(350,350);
		setLayout(null);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent a)
	{
		String text=t.getText();
		if(a.getSource()==button1)
		{
			String words[]=text.split("\\s");
			JOptionPane.showMessageDialog(this,"Total words: "+words.length);
		}
		if(a.getSource()==button2)
		{
			JOptionPane.showMessageDialog(this,"Total Characters including space: "+text.length());
		}
	}
	public static void main(String[] args)
	{
		new CharCount();
	}
}
You can also try this code with Online Java Compiler
Run Code

Output

User has to write some text  to know the word and character count.

Output

Here we input the text “Hello!!! Welcome to Coding Ninjas” 

Output

When a user clicks on the ‘Word’ button using JButton the message pop showing the word count of the text written in the given box.

Output

And, when the user clicks on the ’Character’ Button, a message pop showing the character count including spaces that the user used.

Output

Frequently Asked Questions

In Java, how do you count characters in a string array?

Simply iterate over strings and use length() to determine the number of characters in the word.

Why are Swings used in Java?

Java Swing is a component of the Java Foundation Classes. It is suitable for developing lightweight desktop applications because it is used to create window-based applications.

What is a combobox in Java?

JComboBox is a component of the Java Swing package. JComboBox derives from the JComponent class. JComboBox displays a popup menu with a list of options from which the user can choose.

What is a JButton in Java?

It is used to create a labeled button with platform independence. When the button is pressed, the application performs some action.

Is Swing considered a framework?

Swing is a component-based framework, with all of its components ultimately deriving from the JComponent class.

Conclusion

This blog covered the Word Character Counter in Java. If you would like to learn more, check out these articles on the following to have a better understanding of Java:


Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enroll in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Do upvote our blog to help other ninjas grow. 

Thank you

Happy Learning Ninja! 

Live masterclass