Advertisement
Guest User

CharacterCounter

a guest
Aug 2nd, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class CharacterCounter {
  4.     public static void main(String[] args) {
  5.         // Prompts the user for text and stores a copy in ALL CAPS.
  6.         System.out.print("Please enter a string: ");
  7.         Scanner scan = new Scanner(System.in);
  8.         String userText = scan.next().toUpperCase();
  9.        
  10.         // Creates an array for letters A-Z and stores counts for each letter's occurrence
  11.         // in the user's text.
  12.         int[] capLettersArray = new int[26];
  13.         for (int i = 0; i < userText.length(); i++) {
  14.             char character = userText.charAt(i);
  15.             int value = (int) character;
  16.             if (value >= 65 && value <= 90) {
  17.                 capLettersArray[character - 'A']++;
  18.                 }
  19.             }
  20.        
  21.         // Prints to the console the amount of times each letter (A-Z) is encountered.
  22.         for (int i = 0; i < capLettersArray.length; i++) {
  23.             char character = (char) (i + 65);
  24.             System.out.println(character + " " + capLettersArray[i]);
  25.         }
  26.         scan.close();
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement