Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class CharacterCounter {
- public static void main(String[] args) {
- // Prompts the user for text and stores a copy in ALL CAPS.
- System.out.print("Please enter a string: ");
- Scanner scan = new Scanner(System.in);
- String userText = scan.next().toUpperCase();
- // Creates an array for letters A-Z and stores counts for each letter's occurrence
- // in the user's text.
- int[] capLettersArray = new int[26];
- for (int i = 0; i < userText.length(); i++) {
- char character = userText.charAt(i);
- int value = (int) character;
- if (value >= 65 && value <= 90) {
- capLettersArray[character - 'A']++;
- }
- }
- // Prints to the console the amount of times each letter (A-Z) is encountered.
- for (int i = 0; i < capLettersArray.length; i++) {
- char character = (char) (i + 65);
- System.out.println(character + " " + capLettersArray[i]);
- }
- scan.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement