I'm trying to do this exercise, I do not know where I'm going wrong. Can you help me?
I need to create a map that has the key number of the characters in a city name and a value in a list with all names with that number of characters.
You need to make a print that lists the names of the cities and the number of characters that the names have. Try to have the code as few rows as possible.
How I did it. If you have suggestions for improvements ...
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class testeJava {
public static void main(String[] args) {
HashMap<Integer,String> hm=new HashMap<Integer,String>();
Scanner nomeCidade = new Scanner(System.in);
Scanner qtdCidade = new Scanner(System.in);
System.out.println("Quantidade de Cidade a Adicionar?");
int qtd = qtdCidade.nextInt();
for (int i = 0; i < qtd; i++) {
System.out.println("Informe Nome da Cidade?");
String nome = nomeCidade.nextLine();
hm.put(i,nome);
for (int j = 0; j < nome.length(); j++) {
if(hm.containsKey(nome.charAt(j))){
//ESTA DANDO ERRO NESSA LINHA ABAIXO SOLICITANDO CAST.
hm.put(nome.charAt(j),hm.get(nome.charAt(j))+1);
} else {
hm.put(nome.charAt(j), 1);
}
}
}
for(Map.Entry m:hm.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
nomeCidade.close();
qtdCidade.close();
}
}