Ask coding questions

← Back to all posts
Java HashMap
h
SaunakNandi (1)

Hey I am finding difficulty in detecting the error for this . Can anyone plz tell me how to fix the error . I just want to print the key in alphabetical order

import java.util.*;
import java.util.Map.Entry;
class Charecter
{
// static HashMap<Character,Integer> mp=new HashMap<>();

public static void main(String[] args)
{
    HashMap<Character,Integer> mp=new HashMap<Character,Integer>();
    TreeMap<Character,Integer> tree=new TreeMap<Character,Integer>();
    String str="I love my mom";
    int count;
    
    for(int i=0;i<str.length();i++)
    {
        char ch;
        if(str.charAt(i)!=' ')
        {
            ch=str.charAt(i);
            if(mp.containsKey(ch))
            {
                count=mp.get(ch);
                count++;
                mp.put(ch,count);
            }
            else
                mp.put(ch,1);
        }
    }
    tree.putAll(mp);
    for(Map.Entry<Charecter,Integer> entry : tree.entrySet())
        System.out.println(entry.getKey()+" "+entry.getValue());
}

}

Type mismatch: cannot convert from element type Map.Entry<Character,Integer> to Map.Entry<Charecter,Integer>

Answered by Coder100 (18089) [earned 5 cycles]
View Answer
Comments
hotnewtop
Coder100 (18089)

You made a typo, it is Character not Charecter! Correct code:

        for(Map.Entry<Character,Integer> entry : tree.entrySet())
            System.out.println(entry.getKey()+" "+entry.getValue());
SaunakNandi (1)

@Coder100 thanks Mr. I was using Vscode and didn't expect that. Really I am blind .

Coder100 (18089)

np!

lol we all make typos no worries @SaunakNandi