Please follow me here!

Wednesday, December 5, 2012

String Letter Counter (JAVA Program)

Hello, guys.

This is another JAVA Program I want to share with you guys. It's a counter for a specific letter in an entered string straight from a program.

The code shown here shows how it works.

import javax.swing.JOptionPane;

public class Chapter4LetterCounter
{
    public static void main(String[] args)
    {
        String enterString;
        String characterSearch;
        char characterSearchLetter;
        char existingCharacter;
        int numberOfMatches = 0;
        int enterStringLength;
        int characterCounter = 0;
       
        enterString =
                JOptionPane.showInputDialog("Enter a string. This program will enter how many times a letter in" +
                                            " the string exists.\nPlease note that it will only search for " +
                                            "characters that are distinct from its opposite case partners.");
        enterStringLength = enterString.length();
       
        characterSearch =
                JOptionPane.showInputDialog("What character do you want this program to search?");
        characterSearchLetter = characterSearch.charAt(0);
       
        while (characterCounter < enterStringLength)
        {
            existingCharacter = enterString.charAt(characterCounter);
           
            if (existingCharacter == characterSearchLetter)
            {
                numberOfMatches++;
            }
           
            characterCounter++;
        }
       
        JOptionPane.showMessageDialog(null, "The program has found " + numberOfMatches + " matches of the " 

             + characterSearchLetter + " character.");
       
        System.exit(0);
       
       
    }

}




Google+ Followers

Popular Posts by Gregory