Yes... there is a simpler way. You have two choices, but each about the same. Use an Array, or a Map. The more advanced way of doing this would certainly be with a Map.
Think about a map as a type of array where instead of using an integer to index the array you can use anything. In our case here we'll use char as the index. Because chars are ints you could just use a simple array in this case, and just mentally think of 'a' as 0, but we're going to take the larger step today.
String sample = "Hello World!";
// Initialization
Map <Character, Integer> counter = new HashMap<Character, Integer>();
for(int c = 'a'; c <= 'z'; c++){
counter.put((Character)c, 0);
}
// Populate
for (int i = 0; i < sample.length(); i++){
if(counter.containsKey(sample.charAt(Character)i)))
counter.put(ssample.charAt(i), counter.get(ssample.charAt(i)) + 1 );
}
Now anytime you want to know how many of whatever character there was just call this method
int getCount(Character character){
if(counter.containsKey(character))
return counter.get(character);
else return 0;
}
Note: This only will work for counting punctuation.