Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7890177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:17:14+00:00 2026-06-03T06:17:14+00:00

String playerlist; for(Player p : allplayers){ playerlist += p.getDisplayName() + , ; } sendMessage(playerlist);

  • 0
String playerlist;

for(Player p : allplayers){
    playerlist += p.getDisplayName() + ", ";
}

sendMessage(playerlist);

How can I accomplish this? I need to have a variable inside my for-loop, and then access it outside of the loop.

Thanks, and sorry if this is a very nooby question. I just can’t figure it out.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:17:15+00:00Added an answer on June 3, 2026 at 6:17 am

    You’re close. You need to change the first line to

    String playerlist = "";
    

    In Java it’s illegal to use a variable before it’s initialized, and the line

    playerlist += p.getDisplayName() + ", ";
    

    desugars(*) to

    String temp = playerlist;
    playerlist = temp + (p.getDisplayName() + ", ");
    

    Once you initialize playerlist, you are able to read off its contents into the temporary variable, and then update playerlist.

    There are two higher-level suggestions, though.

    1. If you have a join method available, as some libraries provide (as do the the .NET and Python platforms), you can just use a join, which is easier to read.
    2. However, if you don’t have a join method available (AFAIK, the Java standard library provides no such thing), you should use a StringBuilder, consistent with @anubhava’s suggestion. You’ll get better performance on large numbers of strings, and the code is easier to understand, which is a win-win.

    EDIT: As @edalorzo rightly mentions in the comments, you will only get an error for not initializing a local variable, and both static and instance fields are automatically initialized. Specifically, numeric types (int, long, double, etc.) are initialized to 0, booleans are initialized to false, and reference types are initialized to null.

    The spec explicitly describes the automatic initialization of fields, so you can rely on it, but it’s rarely what you want. Normally, you will want a non-default value of the field when you use it later, since null isn’t terribly useful (and many languages, particularly functional languages like Standard ML, go without null altogether). So I would recommend initializing all the fields of your objects in the constructor.


    (*): The “desugaring” I show above is almost accurate: in truth, playerlist is evaluated only once, which doesn’t affect the behavior of this particular program.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code and it's causing NullPointerException private List<Player> playerList; private
String product = Integer.toString(w); char[] original = String.toCharArray(product); This is the code I have
http://something.php?ownerID=13&view=userAgreement i need to pass two variables to this .. how can i pass
I have a EditText box so a user can input names and then click
String[] a = c.toArray(new String[0]); First: Do I need type cast here? (I think
string a = asdf xyz 123 xx 3212; string seperator = z 1; need
I have this f:viewParam that I try to bind validate and convert a userId
I have an issue, i need to add two class members using a operator
I have an Activity where names are inputed via a EditText box and then
I have a listview that displays files from a directory. The user can drag

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.