Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Trouble Creating Instances with a While Loop

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Thumbs up Trouble Creating Instances with a While Loop

    Salutations ladies and gentlemen! This seems like a helpful forum and I'm very glad it exists. This is my first post here but I've read the rules plus Common Java Mistakes and "Helloworld922's Java Tips." It seems like there are some very knowledgeable people here, and the majority of the advice is way beyond me. XD I've tried to look for similar problems but have not found any, and I have a class to get to shortly. I apologize if this is a redundant thread.

    I'm taking Computer Programming I which centers on Java but it's the professor's first year teaching and I'm trying to stay ahead of the class. We just did "Hello World" a week ago or so. Therefore while I am in class this is not a class problem so there's no academic dishonesty concerns.

    I am trying to create a simple game that asks for a number of players and then creates that many Player objects. I'll try to remember and type the while loop I used, however I do not have my Java textbook with me and the computers in this wing of the school don't have a Java text editor so I apologize for the inevitable errors.

    System.out.println("How many players?");
    Scanner scan = new Scanner();
    int storage = scan.nextInt();
    int counter = 1;
     
    while(counter <= storage) {
         Player player = new Player(counter);
         System.out.println("Welcome Player" + counter + " !");
         counter++;
    }

    Now this prints out what I want but I believe instead of creating multiple Players it just overwrites the same Player repeatedly. I've thought of 2 ways to fix this:

    Brute Force Method: Instead of a while loop create a if/elseif for every possible int value. This seems like a bad idea to me.

    Way I want it to work Method: "Player (player+counter) = new Player(counter);" , where (player+counter) becomes player1 for the first loop, player2 for the second, etc. The problem with this is I don't think it's possible. Does the functionality exist to have a variable in the instantiation of an object like that?

    Again I'm not being paid for this and it's not a school assignment, but I'd like to figure these things out ahead of time so I can help out other people in class if I need too (we have a couple of students in class that don't speak English well and one sits next to me and when he asks me for help and I don't understand or can't help him I feel really bad).

    Thanks in advance for any assistance. I really appreciate the fact a forum like this exists.

    Edit: Thanks again to KevinWorkman! I tried to edit a [Solved] tag to this but can't seem to find the option in the Edit menu.
    Last edited by AbsolutelyNobody; October 12th, 2011 at 06:55 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trouble Creating Instances with a While Loop

    I believe what you're looking for is either an array or an ArrayList. Add each Player instance to the array or ArrayList, then at the end of your loop, each instance will be available in the array.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    AbsolutelyNobody (October 12th, 2011)

  4. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Trouble Creating Instances with a While Loop

    KevinWorkman, thanks for being awesome! We're only on chapter 2 in our book but Arrays were in chapter 8 - I skipped ahead to look them up and it worked like a charm! Thank you very much for you help! I'm sorry it was such a silly problem with such a simple solution, I had no idea how to use arrays before now. Thanks again, and good luck with everything!

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trouble Creating Instances with a While Loop

    No problem. Arrays can be confusing at first, so I'm glad you got it figured out. Nice job taking the initiative and doing some research, glad you got it sorted out.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Trouble using enum in constructor when creating a class
    By willmer in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 13th, 2011, 10:48 AM
  2. Need Help Creating a Loop.
    By chrisnojen117 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 14th, 2011, 10:42 AM
  3. Loop not creating objects
    By xecure in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 10:48 PM
  4. creating a controller to allow instances to be created from keyboard
    By ss7 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 2nd, 2009, 01:30 PM
  5. [SOLVED] Trouble with draw and fillRect in pyramid logic using nested loop
    By LiquidMetal in forum Loops & Control Statements
    Replies: 4
    Last Post: April 27th, 2009, 03:25 AM

Tags for this Thread