• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

stringBuilder

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am stumped. I am trying to append player information from my arrayList called teamList to a string in my displayRoster method. I know I need to use the StringBuilder function to basically add the players information to the string but I don't think I am doing this right. I am using a for loop to run through the arrayList and add each players information and on top of that adding a string escape character so each players information prints on a new line. Right now my only error is the for loop however, when I start messing with my for loop the function under it then gathers errors. I have teamName in my stringBuilder because the output will return the team name and all of the players information, so I basically need to add the players information to this string that already contains the teamName.
 
Saloon Keeper
Posts: 9735
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are not too far off. The syntax of your for() loop is the biggest problem. So, assuming your teamList is an ArrayList<Player> then

For future reference, any time you are seeking help with an issue where an error message is involved, cut and paste the *entire* error message into your post.
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Whenever you're using StringBuilder (or StringBuffer), never ever ever ever use + to concatenate content inside calls to append. Instead, split it up into separate append calls. If possible, use characters instead of single character strings.

So, taking Carey's code:
 
Marshal
Posts: 76862
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are probably better off avoiding \n, which isn't the correct line end for Windows®. Try:-The append method probably calls toString() on its argument, which means if you writeand team happens to be null, you will suffer an exception, but if you writeit will append, “null”.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic