
Hello friends, in this article, we will learn how to create a word counter in Java. Word counter in java can be a simple program and even a GUI app where you just paste some text/paragraph and the Java word counter app will tell you the number of words in the text/paragraph. We will see both one by one. Let’s start with a simple program.
Word Counter in Java Program:
class Main {public static void main(String args[]) {String text = "This is a text used for word counting";String words[] = text.split("\\s");int length = words.length;System.out.println("Number of words are: " + length);}}
Output:
Number of words are: 8
Word Counter app in Java:
import javax.swing.*;import java.awt.event.*;import java.awt.Font;class Main {public static void main(String args[]) {JFrame f = new JFrame("Character Count");JLabel l2, l3, l4;JTextArea text;JLabel l1;JButton submit, clear;text = new JTextArea("");submit = new JButton("SUBMIT");clear = new JButton("CLEAR");l1 = new JLabel("Enter Your string Here : ");l2 = new JLabel("Character with Spaces : ");l3 = new JLabel("Character Without Spaces : ");l4 = new JLabel("Words : ");Font txtFont = new Font(Font.SERIF, Font.PLAIN, 16);l1.setFont(txtFont);l2.setFont(txtFont);l3.setFont(txtFont);l4.setFont(txtFont);l1.setBounds(10, 25, 200, 30);text.setBounds(18, 60, 450, 300);l2.setBounds(10, 370, 400, 30);l3.setBounds(10, 400, 400, 30);l4.setBounds(10, 430, 400, 30);submit.setBounds(100, 470, 100, 50);clear.setBounds(275, 470, 100, 50);text.setLineWrap(true);text.setWrapStyleWord(true);submit.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {String str = text.getText().strip();int count = 0, i, word = 0;l2.setText("Character with Spaces : " + str.length());for (i = 0; i < str.length(); i++) {if (str.charAt(i) != ' ')count++;elseword++;}l3.setText("Character Without Spaces : " + count);l4.setText("Words : " + (word + 1));}});clear.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {text.setText("");l2.setText("Character with Spaces : ");l3.setText("Character Without Spaces : ");l4.setText("Words : ");}});f.add(l1);f.add(text);f.add(l2);f.add(l3);f.add(l4);f.add(submit);f.add(clear);f.setSize(500, 570);f.setResizable(false);f.setLayout(null);f.setLocationRelativeTo(null);f.setVisible(true);f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}}
Output:

Save this article for future, share with your friends
Also Read:
- Dino Game in Java
- Java Games Code | Copy And Paste
- Supply Chain Management System in Java
- Survey Management System In Java
- Phone Book in Java
- Email Application in Java
- Inventory Management System Project in Java
- Blood Bank Management System Project in Java
- Electricity Bill Management System Project in Java
- CGPA Calculator App In Java
- Chat Application in Java
- 100+ Java Projects for Beginners 2023
- Airline Reservation System Project in Java
- Password and Notes Manager in Java
- GUI Number Guessing Game in Java
- How to create Notepad in Java?
- Memory Game in Java
- Simple Car Race Game in Java
- ATM program in Java
- Drawing Application In Java
- Tetris Game in Java
- Pong Game in Java
- Hospital Management System Project in Java
- Ludo Game in Java
- Restaurant Management System Project in Java
- Flappy Bird Game in Java
- ATM Simulator In Java
- Brick Breaker Game in Java
- Best Java Roadmap for Beginners 2023
- Snake Game in Java