1
I’m a beginner in Java. I need a program that reads the name and number of some people and then type a number and the program returns the name of the person associated with the number. I was recommended to use Hashmap. If anyone has an example ...
1
I’m a beginner in Java. I need a program that reads the name and number of some people and then type a number and the program returns the name of the person associated with the number. I was recommended to use Hashmap. If anyone has an example ...
2
import java.util.HashMap;
import java.util.Scanner;
public class Testes {
public static void main(String[] args) {
Scanner leitor = new Scanner(System.in);
HashMap<Integer, String> pessoas = new HashMap<Integer, String>();
int numero;
for(int i = 0; i < 5; i++){
System.out.println("Digite o número da pessoa: ");
numero = leitor.nextInt();
System.out.println("Digite o nome da pessoa: ");
String nome = leitor.next(); // Alteração neste ponto
pessoas.put(numero, nome);
}
System.out.println("Digite o número da pessoa que você deseja buscar: ");
numero = leitor.nextInt();
System.out.println("O nome da pessoa " + numero + " é " + pessoas.get(numero));
}
}
Perfect! Only had a small typing error in the line String name = reader.nextLine(); where it should be next(); Thanks!
Oops, I’m glad I could help! :)
1
The program below shows how to do this.
First, a list is created where objects Pessoas are created and inserted. Next, you scroll through this list and add HashMap as key, the number and as value the person’s name.
Finally, you go through the list again and you recover from the HashMapthe name, using as key the phone.
Note that used Mapas an interface for instantiating the HashMap 'cause that’s good practice.
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HashMapUso {
public static void main(String[] args) {
// crio lista de pessoas
List<Pessoa> pessoas = new ArrayList<Pessoa>();
pessoas.add(new Pessoa("nome1", "numero1"));
pessoas.add(new Pessoa("nome2", "numero2"));
pessoas.add(new Pessoa("nome3", "numero3"));
// mapa onde as pessoas são inseridas
Map<String, String> pessoaMap = new HashMap<String, String>();
// dado um número, guardo o nome
for (Pessoa pessoa : pessoas) {
pessoaMap.put(pessoa.getNumero(), pessoa.getNome());
}
// recupero o nome e o imprimo, dado um número
for (Pessoa pessoa : pessoas) {
System.out.println(pessoaMap.get(pessoa.getNumero()));
}
}
}
class Pessoa {
public Pessoa(String nome, String numero) {
this.nome = nome;
this.numero = numero;
}
private String nome;
private String numero;
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getNumero() {
return numero;
}
public void setNumero(String numero) {
this.numero = numero;
}
}
Browser other questions tagged java hashmap
You are not signed in. Login or sign up in order to post.
http://www.devmedia.com.br/hashmap-java-trabalhando-com-listas-key-value/29811
– ramaral