Word Character Counter in Java with Source Code

Word Character Counter in Java with Source Code

Word Character Counter in Java with Source Code: We can develop Word Character Counter in java with the help of string, AWT/Swing with event handling. Let’s see the code of creating Word Character Counter in java.

  1. String text=“hello javatpoint this is wcc tool”;
  2. String words[]=text.split(“\\s”);
  3. int length=words.length;//returns total number of words
  4. int clength=text.length();//returns total number of characters with space

Let’s see the swing code to count word and character.

  1. import java.awt.event.*;
  2. import javax.swing.*;
  3. public class WCC extends JFrame implements ActionListener{
  4. JTextArea ta;
  5. JButton b1,b2;
  6. WCC(){
  7.     super(“Word Character Counter – JavaTpoint”);
  8.     ta=new JTextArea();
  9.     ta.setBounds(50,50,300,200);
  10.     b1=new JButton(“Word”);
  11.     b1.setBounds(50,300,100,30);
  12.     b2=new JButton(“Character”);
  13.     b2.setBounds(180,300,100,30);
  14.     b1.addActionListener(this);
  15.     b2.addActionListener(this);
  16.     add(b1);add(b2);add(ta);
  17.     setSize(400,400);
  18.     setLayout(null);
  19.     setVisible(true);
  20. }
  21. public void actionPerformed(ActionEvent e){
  22.     String text=ta.getText();
  23.     if(e.getSource()==b1){
  24.         String words[]=text.split(“\\s”);
  25.         JOptionPane.showMessageDialog(this,“Total words: “+words.length);
  26.     }
  27.     if(e.getSource()==b2){
  28.         JOptionPane.showMessageDialog(this,“Total Characters with space: “+text.length());
  29.     }
  30. }
  31. public static void main(String[] args) {
  32.     new WCC();
  33. }
  34. }

Word Count Example with Pad and Text Color

  1. import java.awt.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. public class CharCount extends JFrame implements ActionListener{
  5.     JLabel lb1,lb2;
  6.     JTextArea ta;
  7.     JButton b;
  8.     JButton pad,text;
  9.     CharCount(){
  10.         super(“Char Word Count Tool – JTP”);
  11.         lb1=new JLabel(“Characters: “);
  12.         lb1.setBounds(50,50,100,20);
  13.         lb2=new JLabel(“Words: “);
  14.         lb2.setBounds(50,80,100,20);
  15.         ta=new JTextArea();
  16.         ta.setBounds(50,110,300,200);
  17.         b=new JButton(“click”);
  18.         b.setBounds(50,32080,30);//x,y,w,h
  19.         b.addActionListener(this);
  20.         pad=new JButton(“Pad Color”);
  21.         pad.setBounds(140,320110,30);//x,y,w,h
  22.         pad.addActionListener(this);
  23.         text=new JButton(“Text Color”);
  24.         text.setBounds(260,320110,30);//x,y,w,h
  25.         text.addActionListener(this);
  26.         add(lb1);add(lb2);add(ta);add(b);add(pad);add(text);
  27.         setSize(400,400);
  28.         setLayout(null);//using no layout manager
  29.         setVisible(true);
  30.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  31.     }
  32.     public void actionPerformed(ActionEvent e){
  33.         if(e.getSource()==b){
  34.         String text=ta.getText();
  35.         lb1.setText(“Characters: “+text.length());
  36.         String words[]=text.split(“\\s”);
  37.         lb2.setText(“Words: “+words.length);
  38.         }else if(e.getSource()==pad){
  39.             Color c=JColorChooser.showDialog(this,“Choose Color”,Color.BLACK);
  40.             ta.setBackground(c);
  41.         }else if(e.getSource()==text){
  42.             Color c=JColorChooser.showDialog(this,“Choose Color”,Color.BLACK);
  43.             ta.setForeground(c);
  44.         }
  45.     }
  46. public static void main(String[] args) {
  47.     new CharCount();
  48. }}

word count tool

Next TopicURL Source Code Generat
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

Word Character Counter in Java with Source Code Word Character Counter in Java with Source Code: We can develop Word Character Counter in java with the help of string, AWT/Swing with event handling. Let’s see the code of creating Word Character Counter in java. String text=“hello javatpoint this is wcc tool”; String words[]=text.split(“\\s”); int length=words.length;//returns total number of words int clength=text.length();//returns total number of characters with space Let’s see the swing code to count word […]

Tags:

....
...