Hey all so here is the problem that I'm asked to solve:
Write a program that will read in a line of text and output a list of all the letters that occur in the text, along with the number of times each letter occurs. End the line with a period that serves as a sentinel value. The letters should be listed in alphabetical order when they are output. Use an array of base type int of length 26, so that each indexed variable contains the count of how many letters there are. Array indexed variable 0 contains the number of a’s, array indexed variable 1 contains the number of b’s and so forth. Allow both uppercase and lowercase letters as input, but treat uppercase and lowercase versions of the same letter as being equal.
Here is my code so far
import java.util.Scanner;
public class Letters {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int letter[] = new int[26];
System.out.println("Enter in a list of letters");
String s = keyboard.nextLine();
String a = s.toUpperCase();
System.out.println(a);
for(int i = 0; i<s.length();i++){
switch (a.charAt(i)){
case 'A':letter[0]++;
break;
case 'B': letter[1]++;
break;
case 'C':letter[2]++;
break;
case 'D': letter[3]++;
break;
case 'E':letter[4]++;
break;
case 'F': letter[5]++;
break;
case 'G':letter[6]++;
break;
case 'H': letter[7]++;
break;
case 'I':letter[8]++;
break;
case 'J': letter[9]++;
break;
case 'K':letter[10]++;
break;
case 'L': letter[11]++;
break;
case 'M':letter[12]++;
break;
case 'N': letter[13]++;
break;
case 'O':letter[14]++;
break;
case 'P': letter[15]++;
break;
case 'Q':letter[16]++;
break;
case 'R': letter[17]++;
break;
case 'S':letter[18]++;
break;
case 'T': letter[19]++;
break;
case 'U':letter[20]++;
break;
case 'V': letter[21]++;
break;
case 'W':letter[22]++;
break;
case 'X': letter[23]++;
break;
case 'Y':letter[24]++;
break;
case 'Z': letter[25]++;
break;
}
}
}
}
Clearly, I'm not done. So I'm solving this problem one step at a time and the problem I'm having at the moment is to find a way to display the number of times all the letters appeared in the user input without displaying the letters that were not inputted. My current idea is use a bunch of if statements but I know that will get messy super quick.