0

im new to Java and I'm trying to add an object to ma ArrayList like this:

ArrayList<Wizytowka> baza = new ArrayList<Wizytowka>();
int nrTel = 123456789;
Wizytowka nowa = new Wizytowka("imie","nazwisko","nazwaFirmy","mail",nrTel);
baza.add(nowa);
System.out.println("Baza:\n"+baza);

But the output is weird and looks like this:

Baza:
[com.company.Wizytowka@31cefde0]

I really don't know what is wrong. When I try to just output it from object nowa with nowa.wypisz() it works just fine. Here's how my class "Wizytowka" looks:

public class Wizytowka {
private String imie;
private String nazwisko;
private String nazwaFirmy;
private String mail;
private int nrTel;

//imie
public void setImie(String imie){
    this.imie = imie;
}
public String getImie(){
    return this.imie;
}
//nazwisko
public void setNazwisko(String nazwisko){
    this.nazwisko = nazwisko;
}
public String getNazwisko(){
    return this.nazwisko;
}
//nazwa firmy
public void setNazwaFirmy(String nazwaFirmy){
    this.nazwaFirmy = nazwaFirmy;
}
public String getNazwaFirmy(){
    return this.nazwaFirmy;
}
//mail
public void setMail(String mail){
    this.mail = mail;
}
public String getMail(){
    return this.mail;
}
//nr telefonu
public void setNrTel(int nrTel){
    this.nrTel = nrTel;
}
public int getNrTel(){
    return this.nrTel;
}

public Wizytowka(String imie, String nazwisko, String nazwaFirmy, String mail, int nrTel){
    setImie(imie);
    setNazwisko(nazwisko);
    setNazwaFirmy(nazwaFirmy);
    setMail(mail);
    setNrTel(nrTel);
}

public void wypisz(){
    System.out.println("Imie: "+this.imie);
    System.out.println("Nazwisko: "+this.nazwisko);
    System.out.println("Firma: "+this.nazwaFirmy);
    System.out.println("Mail: "+this.mail);
    System.out.println("Nr tel.: "+this.nrTel);
}
}

Thank you for any help

Leotix
  • 1
  • 2

2 Answers2

1

override toString() method as below :-

   @Override
public String toString() {
    return "Wizytowka [imie=" + imie + ", nazwisko=" + nazwisko + ", nazwaFirmy=" + nazwaFirmy + ", mail=" + mail
            + ", nrTel=" + nrTel + "]";
}

public class Wizytowka {
    private String imie;
    private String nazwisko;
    private String nazwaFirmy;
    private String mail;
    private int nrTel;

    //imie


public Wizytowka(String imie, String nazwisko, String nazwaFirmy, String mail, int nrTel){
    setImie(imie);
    setNazwisko(nazwisko);
    setNazwaFirmy(nazwaFirmy);
    setMail(mail);
    setNrTel(nrTel);
}

public void wypisz(){
    System.out.println("Imie: "+this.imie);
    System.out.println("Nazwisko: "+this.nazwisko);
    System.out.println("Firma: "+this.nazwaFirmy);
    System.out.println("Mail: "+this.mail);
    System.out.println("Nr tel.: "+this.nrTel);
}
@Override
public String toString() {
    return "Wizytowka [imie=" + imie + ", nazwisko=" + nazwisko + ", nazwaFirmy=" + nazwaFirmy + ", mail=" + mail
            + ", nrTel=" + nrTel + "]";
}



}
azro
  • 53,056
  • 7
  • 34
  • 70
Gaurav Dhiman
  • 953
  • 6
  • 11
0

You have to override toString method of your class Wizytowka, if you want to see content of object (not it's class name and address, as in default toString implementation used from Object class). There are more details in tutorial.

To avoid boilerplate code you may want to use @ToString feature from lombok.

y_ug
  • 904
  • 6
  • 8