Wednesday, April 8, 2020

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.       
  11.     b1=new JButton("Word");  
  12.     b1.setBounds(50,300,100,30);  
  13.       
  14.     b2=new JButton("Character");  
  15.     b2.setBounds(180,300,100,30);  
  16.       
  17.     b1.addActionListener(this);  
  18.     b2.addActionListener(this);  
  19.     add(b1);add(b2);add(ta);  
  20.     setSize(400,400);  
  21.     setLayout(null);  
  22.     setVisible(true);  
  23. }  
  24. public void actionPerformed(ActionEvent e){  
  25.     String text=ta.getText();  
  26.     if(e.getSource()==b1){  
  27.         String words[]=text.split("\\s");  
  28.         JOptionPane.showMessageDialog(this,"Total words: "+words.length);  
  29.     }  
  30.     if(e.getSource()==b2){  
  31.         JOptionPane.showMessageDialog(this,"Total Characters with space: "+text.length());  
  32.     }  
  33. }  
  34. public static void main(String[] args) {  
  35.     new WCC();  
  36. }  
  37. }  

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.           
  16.         ta=new JTextArea();  
  17.         ta.setBounds(50,110,300,200);  
  18.           
  19.         b=new JButton("click");  
  20.         b.setBounds(50,32080,30);//x,y,w,h  
  21.         b.addActionListener(this);  
  22.       
  23.         pad=new JButton("Pad Color");  
  24.         pad.setBounds(140,320110,30);//x,y,w,h  
  25.         pad.addActionListener(this);  
  26.   
  27.         text=new JButton("Text Color");  
  28.         text.setBounds(260,320110,30);//x,y,w,h  
  29.         text.addActionListener(this);  
  30.   
  31.         add(lb1);add(lb2);add(ta);add(b);add(pad);add(text);  
  32.           
  33.         setSize(400,400);  
  34.         setLayout(null);//using no layout manager  
  35.         setVisible(true);  
  36.         setDefaultCloseOperation(EXIT_ON_CLOSE);  
  37.     }  
  38.     public void actionPerformed(ActionEvent e){  
  39.         if(e.getSource()==b){  
  40.         String text=ta.getText();  
  41.         lb1.setText("Characters: "+text.length());  
  42.         String words[]=text.split("\\s");  
  43.         lb2.setText("Words: "+words.length);  
  44.         }else if(e.getSource()==pad){  
  45.             Color c=JColorChooser.showDialog(this,"Choose Color",Color.BLACK);  
  46.             ta.setBackground(c);  
  47.         }else if(e.getSource()==text){  
  48.             Color c=JColorChooser.showDialog(this,"Choose Color",Color.BLACK);  
  49.             ta.setForeground(c);  
  50.         }  
  51.     }  
  52. public static void main(String[] args) {  
  53.     new CharCount();  
  54. }}  
word count tool

No comments:

Post a Comment

CORBA Java Tutorial using Netbeans and Java 8.

CORBA-Example A simple CORBA implementation using Java Echo.idl module EchoApp{ interface Echo{ string echoString(); }; }; ...