Skip to content
Advertisement

Static method to count occurrences of a letter in a lower case word using maps

I have to create a class called MakeMap with a single static method called countLetters(). The method should take as a parameter a word formed of lowercase letters and return a map of the counts of the letters in the word. This is the test code for the Main class (which I cannot edit):

JavaScript

The output must be formatted like this also: $ java Main There are 1 of the letter ‘e’ There are 1 of the letter ‘h’ There are 2 of the letter ‘l’ There are 1 of the letter ‘o’

This is what I have attempted so far in my MakeMap class. I feel like I only need a couple of minor adjustments to get this working.

JavaScript

This is the error message I am getting: Note: Main.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details.

Advertisement

Answer

Incorrect brackets, should be:

charCountMap.put(word.charAt(i), (charCountMap.get(word.charAt(i)) + 1));

additionaly you must return charCountMap from this method isntead of print it out there, so the method must be defined as public static Map<Character, Integer> countLetters(String word)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement