Here's my code. I need to figure out how to print the specific character in the string (say I input word5 - the the erroneous character would be the 5) that keeps the code from working.
<code>
// ************************************************** **************
// CountLetters.java
//
// Reads a words from the standard input and prints the number of
// occurrences of each letter in that word.
//
// ************************************************** **************
import java.util.Scanner;
public class CountLetters
{
public static void main(String[] args)
{
int[] counts = new int[26];
Scanner scan = new Scanner(System.in);
// Read word from user
System.out.print("Enter a single word (letters only, please): ");
String word = scan.nextLine();
// Convert input to all upper case
word = word.toUpperCase();
// Count frequency of each letter in string
try {
for (int index=0; index < word.length(); index++)
counts[word.charAt(index)-'A']++;
// Print frequencies of letters
System.out.println();
for (int index=0; index < counts.length; index++)
if (counts [index] != 0)
System.out.println((char)( index +'A') + ": " + counts[index]);
}
catch (ArrayIndexOutOfBoundsException e) {
System.err.println( word +" is not just letters, dude.");
}
}
}
</code>