Bangladeshi Softwares
Home - Download - Codes & Tutorials - Bangla IT Bibhag - Mobile - About Me

 


Trying some methods of Character class in an Applet


Step-1:
This code snippet utilizes some methods of the java.lang.Character class. Some methods may be deprecated in the latest version of Sun's JDK (this code was in the download section and originally written in the year 2002). I used JDK 1.3.1_01 to compile this class. Open your favorite text editor and type the code below:


/*
 * Author: Ahsanul Haque Shovon
 * Email: ahsanul.haque.shovon@gmail.com / ahsanul_haque_shovon@yahoo.com
 * Web: http://shovon.110mb.com
 */

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class CharacterTest extends Applet implements ActionListener
{
  TextArea typeParagraph;
  Button calculate;
  Button clear;

  int countUppercase;
  int countLowercase;
  int countWhitespace;
  int countNumber;
  int countSpecialCharacter;
  int countCharacter;
  int countWord;
  String temp;

	
  public void init()
  {
    setLayout(new FlowLayout());

    typeParagraph = new TextArea("This is a sample text.", 6, 30);
    add(typeParagraph);

    calculate = new Button("Calcute");
    calculate.addActionListener(this);
    add(calculate);

    clear = new Button("Clear");
    clear.addActionListener(this);
    add(clear);
  }


 /***********************************
  * Method: restore_value
  * Purpose: Restores values to their
  * original state.
  ***********************************/
  public void restore_values()
  {
    countUppercase = 0;
    countLowercase = 0;
    countWhitespace = 0;
    countNumber = 0;
    countSpecialCharacter = 0;
    countCharacter = 0;
    countWord = 0;
  }
	

  public void actionPerformed(ActionEvent ae)
  {
    if(ae.getSource() == calculate)
    {
      temp = typeParagraph.getText();
      countCharacter = temp.length();

      for(int i = 0; i < temp.length(); i++)
      {
	if(Character.isUpperCase(temp.charAt(i)))
        {
	  ++countUppercase;
        }

	if(Character.isLowerCase(temp.charAt(i)))
        {
	  ++countLowercase;
        }

	if(Character.isWhitespace(temp.charAt(i)))
        {
	  ++countWhitespace;
        }

	if(Character.isDigit(temp.charAt(i)))
        {
	  ++countNumber;
        }

	if(!Character.isLetterOrDigit(temp.charAt(i)))
	{
	  if(!Character.isWhitespace(temp.charAt(i)))
          {
	    ++countSpecialCharacter;
          }
	}

	if(Character.isWhitespace(temp.charAt(i)) &&
          (Character.isLetterOrDigit(temp.charAt(i+1))))
        {
	  ++countWord;
        }
      }

      repaint();
    }

    if(ae.getSource() == clear)
    {
      typeParagraph.setText("");
      restore_values();
      repaint();
    }
  }
	

  public void paint(Graphics g)
  {
    showStatus(getAppletInfo());

    g.drawString("Total capital letters : " + countUppercase, 10, 160);
    g.drawString("Total small letters : " + countLowercase, 10, 180);
    g.drawString("Total white spaces : " + countWhitespace, 10, 200);
    g.drawString("Total numbers : " + countNumber, 10, 220);
    g.drawString("Total special characters : " + countSpecialCharacter,
                  10, 240);
    g.drawString("Total characters : " + countCharacter, 10, 260);

    if(countCharacter != 0)
    {
      countWord += 1;
    }

    g.drawString("Total words : " + countWord, 10, 280);
    restore_values();
  }


  public String getAppletInfo()
  {
    //You are not authorized to remove the code below
    return "İAhsanul Haque Shovon";
    //You are not authorized to remove the code above
  }
}


Step-2:
Save the above code as "CharacterTest.java" and use the following command to compile it:

javac -g:none -verbose CharacterTest.java

And use the following code to embed the class file in a HTML page:

<applet code="CharacterTest.class" width="230" height="300">
 Your browser does not support Java Applets or is not a Java enabled
 browser. Please download The latest Java plug-in from
 <a href="http://www.java.com/" target="_new">http://www.java.com</a>
</applet>

If you do not want to compile the code for some reason, you can download the pre-compiled CharacterTest.class file here.

Your browser does not support Java Applets or is not a Java enabled browser. Please download The latest Java plug-in from http://www.java.com


Other Helpful Resources:
http://java.sun.com
http://javaboutique.internet.com



 



©Ahsanul Haque Shovon
All rights reserved