4

I need some advice. I created three classes Om(Human), Parinte(Father) and Copil(Child). I have created constructors of this classes. In main class i have created one instance for every class. Now i stored my objects in a ArrayList. After this i will create some methods to treat my objects like sorting them A to Z by name. My question is, how to print these objects into a clear String format after i will call my sort methods cause i'm getting every time stuff like @... when i try simply to print a value stored in my collection?

My code: Om(Human) class:

public class Om {
String nume;
String prenume;
String cnp;

Om(String nume,String prenume,String cnp){
this.nume=nume;
this.prenume=prenume;
this.cnp=cnp;
}
}

My Parinte(Father) class:

public class Parinte extends Om{
String domiciliu;
Parinte(String nume, String prenume, String cnp, String domiciliu){
    super(nume,prenume,cnp);
    this.domiciliu=domiciliu;
}
}

My Copil(Child) class:

public class Copil extends Parinte {
int varsta;
Copil(String nume, String prenume, String cnp, String domiciliu, int varsta){
    super(nume,prenume,cnp,domiciliu);
    this.varsta=varsta;
}
}

And finally my main class:

import java.util.ArrayList;
import java.util.Arrays;
public class main {

public static void main(String[] args) {
    Om om1=new Om("Dubolari", "Dragos", "2006004034120");
    Parinte p1=new Parinte("Enache","Andrei","2006004034120","Al. Cel Bun 13");
    Copil c1 = new Copil("Enache","Valeriu","2006004034120","Al. Cel Bun 13",15);
    ArrayList<Om> colectie = new ArrayList<Om>();
    colectie.add(om1);
    colectie.add(p1);
    colectie.add(c1);

}
}
drgPP
  • 926
  • 2
  • 7
  • 22
  • How do you expect your **custom** value to be printed and why? – Sotirios Delimanolis Mar 25 '14 at 21:01
  • 1
    Override the toString method in your classes. – Alexis C. Mar 25 '14 at 21:01
  • I expect my values to be printed like a table. For example with my sort method i can organise them A to Z by name. About overriding, can you be please a bit more explicit cause i've never used them and where i can read about this? – drgPP Mar 25 '14 at 21:05

4 Answers4

2

You'll need to override the toString() of your objects. Here is an example:

public class Copil extends Parinte {
    int varsta;
    Copil(String nume, String prenume, String cnp, String domiciliu, int varsta){
        super(nume,prenume,cnp,domiciliu);
        this.varsta=varsta;
    }

    @Override
    public String toString() {
        return "Copil. Nume" + nume + " Varsta: " + varsta;  //Whatever you want to do here
    }

}

Then you could iterate over your collection and print the objects:

for(Om o: colectie){
    System.out.println(o);  //printing calls the toString()
}
Tyler
  • 17,669
  • 10
  • 51
  • 89
  • Thank you very much, i will try it till ill get the right outputs. – drgPP Mar 25 '14 at 21:08
  • Your welcome. You could also override the `toString()` in your base class, depending on what exactly you're trying to print. – Tyler Mar 25 '14 at 21:11
1

When you try to print out the contents of the list, the java run time basically calls the implicitly inherited toString() method of every class. Since you have not overriden this method, it calls the default implementation which prints out in a format that you might not expect (string beginning with @)

ucsunil
  • 7,378
  • 1
  • 27
  • 32
1

You can try using FastCode template, where you can print fields of a class. You can edit the template easily as per your requirement. Please refer the documentation which explains how to create new/edit templates.

For example, in fastcode template you can choose variations like logger.debug, logger.trace, System.out.println, so ..on to print the variables. When you select the template and class, you get all the variables present in class to select for printing as shown in snapshot. Once you select all the variables you get the below code generated. You can customize the output as per your requirement.

System.out.println("args " + args) ;
System.out.println("c1 " + c1) ;
System.out.println("colectie " + colectie) ;
System.out.println("om1 " + om1) ;
System.out.println("p1 " + p1) ;

enter image description here

LeenaLatesh
  • 169
  • 1
  • 3
  • 12
0

The @number is the unsigned hexadecimal representation of the hashcode of your object.

Override the toString to get meaningful representation

How to override toString() properly in Java?

Community
  • 1
  • 1
Ajay George
  • 11,759
  • 1
  • 40
  • 48