• Post Reply Bookmark Topic Watch Topic
  • New Topic

Struggling to Display Entire Element in ArrayList Object  RSS feed

 
Imzy Alli
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I've created an ArrayList object with multiple variables. I've used the toString method to override one of the variables in the element of the object but I don't know how to print the other variables. My code is as follows:





Output is as follows:

The list contains:
John


I want to display the other variables in the element but I'm stuck on how to do that. Any help is appreciated. Thank you
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You are only printing a single field because that is all you are returning from the toString method. You can return the other fields, possibly with the String#format method, but you will get a longer printout.Go through the Formatter documentation and these two Java™ Tutorials sections: 1 2 to find out about formatting. Note those links may use format Strings for printf, but you use exactly the same format Strings for String#format.
 
Scott Shipp
Ranch Hand
Posts: 223
12
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try checking out the Java Trails on Oracle's site. Here is the one on Formatting which is most relevant to your question. Hope that helps.
 
Scott Shipp
Ranch Hand
Posts: 223
12
Eclipse IDE IntelliJ IDE Java Scala Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, the one on Strings may be more useful for you. I misspoke.
 
Imzy Alli
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Welcome to the Ranch

You are only printing a single field because that is all you are returning from the toString method. You can return the other fields, possibly with the String#format method, but you will get a longer printout.Go through the Formatter documentation and these two Java™ Tutorials sections: 1 2 to find out about formatting. Note those links may use format Strings for printf, but you use exactly the same format Strings for String#format.


Hi, thank you for the welcome and quick response.

I've implemented the String.format method now, yet it still only outputs the first field in the element. 
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imzy Alli wrote:
Campbell Ritchie wrote:You are only printing a single field

I've implemented the String.format method now, yet it still only outputs the first field in the element. 

If you only print one field as Campbell was saying, why would expect it to print anything else? Your String.format call needs to include everything you want to display.
 
Imzy Alli
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:
Imzy Alli wrote:
Campbell Ritchie wrote:You are only printing a single field

I've implemented the String.format method now, yet it still only outputs the first field in the element. 

If you only print one field as Campbell was saying, why would expect it to print anything else? Your String.format call needs to include everything you want to display.


I did include the call to everything I wanted to display but it was still only printing the name. I corrected it by changing my code to:



all the fields print now.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were given a link to the API documentation for String.format the Formatter documentation -- you should have clicked on that link and read the documentation because the way you did it was incorrect. Your "fix" could have been better if you stuck with the correct usage of String.format(), IMO.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!