Count Number of Each Letter in Given Word?
Hi, So I'm trying to write a program in which the user enters a word, then the program displays each letter of the alphabet and how many times each letter is used in that word.
Ex. "Testing" A-0 B-0 .... S-1 T-2
Like that
However, I don't know whats wrong with the code and how to fix it?
Any help is appreciated :)
Code :
import java.lang.String;
public class Words
{
public static void main(String[]args)
{
String words = new String();
words = Input.getString("Please enter a statement");
int[]total = totalLetters(words.toLowerCase());
for (int i=0; i < total.length; i++)
{
if (total[i]!=0)
System.out.println("Letter " + (char)('a' + i) + " count = " + total[i]);
}
}
public static int[]totalLetters(String words)
{
int[]total = new int[26];
for (int i=0; i < words.length(); i++)
{
if (Character.isLetter(words.charAt(i)))
{
total[words.charAt(i) - 'a']++ ;
}
}
return total;
}
}
Re: Count Number of Each Letter in Given Word?
Quote:
However, I don't know whats wrong with the code
What makes you think there's something wrong with your code?
Re: Count Number of Each Letter in Given Word?
I keep getting an error message about "input"
Re: Count Number of Each Letter in Given Word?
Please post the full text of your error message here so we can see it.