2
public class Alimento {

private String nome;
private int quantità;
private String scadenza;
private String tipo;


Alimento(JTextField txt_nome, JTextField txt_quantità, JTextField txt_scadenza, JTextField txt_tipo) {
    setNome(txt_nome.getText()); 
    setQuantità(Integer.parseInt(txt_quantità.getText()));
    setScadenza(txt_scadenza.getText());
    setTipo(txt_tipo.getText());

    /*System.out.println(getNome());
    System.out.println(getQuantità());
    System.out.println(getScadenza());
    System.out.println(getTipo());*/

}

public String getNome(){
    return nome;
}
public int getQuantità(){
    return quantità;
}
public String getScadenza(){
    return scadenza;
}

public String getTipo(){
    return tipo;
}

public String setNome(String name){
    return nome = name;
}

public int setQuantità(int quant){
    return quantità = quant;
}

 public String setTipo(String type){
    return tipo = type;
}

public String setScadenza(String scad){
    return scadenza = scad;
}

}




public class Dispensa{

LinkedList<Alimento> dispensa = new LinkedList<>();

public Dispensa(){}

public void add(Alimento alimento){
  dispensa.add(alimento);
}
public void showList(){
for (int i = 0; i < dispensa.size(); i++)
    System.out.println(dispensa);
}
}

when i try to display the elements i have the numeric adress of them as (mykitchen.Alimento@10a6cf9e). i would display something like:

name:...

quantità:...

for each element that i have into the linked list

thank you!

Rob
  • 2,649
  • 3
  • 27
  • 34
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – Gerardo BLANCO Feb 27 '18 at 18:30

1 Answers1

1

It's normal to get the object reference like that mykitchen.Alimento@10a6cf9e because you call a toString method for an object in the line System.out.println(dispensa);, the dispensa object is an List of alimento object, So you have to get the alimento object in the loop using dispensa.get(index), the call the getter that you want. Like :

public void showList(){
for (int i = 0; i < dispensa.size(); i++)
  {
    Alimento  alimento = dispensa.get(i);
    System.out.println(alimento.getNome());
  }
}

You can also define the toString() for Alimento in the class so you wouldn't have to call getNome().

clinomaniac
  • 2,200
  • 2
  • 17
  • 22
Nomade
  • 1,760
  • 2
  • 18
  • 33