How do I display what’s on the list?

Asked

Viewed 1,007 times

3

I have this method that adds things to the list:

 public Pedido adicionarPedido(int quantidade, String nome){

        lista.add(new Item(quantidade, nome ));
            return this;
 }

And I’ve entered that class into the list attributes :

List<Item> lista = new ArrayList<Item>();

I am asking to display in the toString method the list data plus the person’s name:

     public Pedido ToString() {
        mensagem = "Pedido [lista=" + lista.get(0) +  lista.get(1) + ", cliente=" + cliente.getNome() + "]";
          return this;
     }

The problem is that the console is being printed like this:

Pedido [lista=teste.Item@15db9742 teste.Item@6d06d69c, cliente=Aline Gonzaga]

But I want the list items printed out.

How can I do that?

  • 1

    How’s your class Request?

  • 1

    What should I print? and I reinforce the request to post the class Pedido.

2 answers

4

To me it’s an abuse to use the ToString() for this, this method was not created for this kind of thing.

You should take the element and the class member, and you should do it by sweeping the list, something like that:

public Pedido ToString() {
     mensagem = "Pedido do Cliente " + cliente.getNome() + "\n";
     for (Item item : lista) mensagem += "Quantidade: " + item.quantidade + " Produto: " + item.nomeProduto + "\n";
     return this;
}

I put in the Github for future reference.

I could only answer by looking at your previous question. All this code still has several other problems.

  • I also find it an abuse to chain methods this way, seemingly without need, making return to their own class, but it is the choice of the OP, just to warn even.

  • 1

    @diegofm actually has it too, but I’ve already spoken in another answer.

  • I learned this from my teacher. Play with her. rs

  • 4

    @gonz tells her to come here, I find it unlikely that she is teaching wrong, but if she is she can justify why she is doing this, or learn to do right.

  • 5

    @gonz just because she’s a teacher doesn’t mean she owns the reason, I’ve seen teachers teach the way they think is right, but then I realized it wasn’t like that. We have to be open-minded to knowledge, and not cast ourselves in the ideas that impose us, so we can get a wider range of resources, regardless of what language or area it is.

3


Your class Item need to overwrite the method toString. By default the method shows a hashing unique generated by JDK. Then it would be something like:

@Override
public String toString() {
  return String.valueOf(this.getQuantidade()) + " " + this.getNome();
}

Whenever a method that receives a String as parameter and an object is passed, the method toString is invoked. In the case of a list, the method of your items is invoked, causing the effect you described (Item@...).

Right after you can also overwrite the order class method:

@Override
public String toString() {
  return "Pedido: lista=" + this.getLista() + ", cliente=" + this.getCliente().getNome();
}

So the following call:

System.out.println(pedido);

Will produce:

Pedido: lista=[1 Produto 1, 2 Produto 2], cliente=Aline Gonzaga

I also noticed that you used the method ToString capital letters. By the code convention Java methods should start with lower case letter, except constructors. Then you should override the method toString.

Regarding the use and suggestion on the abuse of toString in this answer, we have this topic in stackoverflow which indicates that the use is interesting for debugging purposes, so maybe to show a final message in your application is better another method to define the message.

  • 2

    Access properties directly? And it’s not memory address, it’s a unique hashing generated by JDK.

  • 2

    This is the problem of answering questions before the OP add further clarifications. Missing information in the answer.

  • It worked thanks friend

  • 1

    Now you could explain to me why it worked, like I just added Tostring() to the Item class and it worked...

  • Great, thanks. I get it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.