I’m having to make a phonebook for a exercise and I need to do it using ArrayList, I added the option to register users in the menu, but when printing these users, it gives me something like: [Contact@68e09d7b, Contact@51da2082]
I researched here on Stack Overflow and saw people who had problems similar to me, and in the answers, people told them to use the Override toString() method in the main class, but when I tried to do that, my code returned both lists within a list.
Output:
[Jon, 99999-9999, jon@gmail.com, 01/01/2000, Tom, 99999-9998, tom@gmail.com, 02/01/2000]
Expected:
[Jon, 99999-9999, jon@gmail.com, 01/01/2000]
[Tom, 99999-9998, tom@gmail.com, 02/01/2000]
Here are the two classes I used to make the program.
Contact.class
public class Contato {
// Atributos da classe Contato
private String id;
private String nome;
private String telefone;
private String email;
private String aniversario;
public String getID() {
return id;
}
public void setID(String id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getTelefone() {
return telefone;
}
public void setTelefone(String telefone) {
this.telefone = telefone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAniversario() {
return aniversario;
}
public void setAniversario(String aniversario) {
this.aniversario = aniversario;
}
public Contato(String nome, String telefone) {
this.id = id;
this.nome = nome;
this.telefone = telefone;
this.email = "Email not entered";
this.aniversario = "Birthday date not entered";
}
@Override
public String toString() {
return nome + ", " + telefone + ", " + email + ", " + aniversario;
}
}
Main.class
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
public class Main{
public static void main(String args[]){
Scanner entrada = new Scanner(System.in);
String id, nome, telefone, email, aniversario;
ArrayList<Contato> contatos = new ArrayList<>();
Agenda agenda = new Agenda();
while(true){
System.out.println("\n1. Register New Contact");
System.out.println("2. Search Contact");
System.out.println("3. Delete Contact");
System.out.println("4. Exit");
System.out.print("Your choice: ");
int opcao = Integer.parseInt(entrada.nextLine());
switch(opcao){
case 1:
System.out.print("Name: ");
nome = entrada.nextLine();
System.out.print("Phone number: ");
telefone = entrada.nextLine();
Contato novoContato = new Contato(nome, telefone);
System.out.print("E-mail: ");
email = entrada.nextLine();
novoContato.setEmail(email);
System.out.print("Birthday date: ");
aniversario = entrada.nextLine();
novoContato.setAniversario(aniversario);
contatos.add(novoContato);
break;
case 2:
System.out.println(contatos);
break;
}
}
}
}
>Solution :
Instead of trying to print the list, print each contact in turn separately
for (Contato contato : contatos) {
System.out.println("[" + contato + "]");
}
You could also make use of something like StringJoiner
, for example…
StringJoiner sj = new StringJoiner("\n");
for (Contato contato : contatos) {
sj.add(contato.toString());
}
System.out.println(sj);
Although, for performance, I’d probably stick to just looping over the list