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 8 of 8

Thread: instantiating a object

  1. #1
    Junior Member Neo's Avatar
    Join Date
    Feb 2011
    Location
    Ohio
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default instantiating a object

    so im trying to write a program that tracks the stats of different soccer players. the program will create an object of player that holds the information of first name last name, points, assists, and penatly kick percentage. it will show the information, it will remove the player if needed or requested, it will save the information within the created arraylist to a file of the user decided name. it will quit just fine. the problem i have is when i try to create a new instance of the player object it just overwrites the first player object. now in our guidelines of the project it doesn't say how many players you have to have room for. i tried using an updateable counter of integer. but when you type in Player player (counter) = new Player (); it gives all kinds of errors. i even tried it with a string variable, same issue. i am not privy to the total number of players allowed to be entered or not. so aside from hard coding in 1,000,000 player objects with incrimenting names i have no clue how to allow it to update itself. any ideas?
    Last edited by Neo; April 17th, 2011 at 09:52 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: instantiating a object

    Use a container. Java has a bunch of containers for different applications. If you just want a list of players, try using an ArrayList. If you want to associate each player with some "key" (for example, the player's name), use a HashMap.

    ArrayList<Player> players = new ArrayList<Player>();
    // add a few players. Note that you can add as many as you want
    players.add(new Player());
    players.add(new Player());
    players.add(new Player());

    For more on ArrayLists, see the JavaDoc: ArrayList (Java Platform SE 6)

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

    Neo (April 18th, 2011)

  4. #3
    Junior Member Neo's Avatar
    Join Date
    Feb 2011
    Location
    Ohio
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: instantiating a object

    would just adding a new player not overwrite the previous player created??? thats my current problem, it will create the one player object fine, but when you go and select add another player it overwrites it. so how would i let the program know that the next player is to be added to the list not overwriting the list. and sorry to sound stupid, but what's a hashmap? haven't learned about them yet, i don't think
    Last edited by Neo; April 18th, 2011 at 10:54 PM.

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: instantiating a object

    You create new and unique objects rather than changing the properties of the same object.
    class Foo {
        int value;
     
        public void set(int v) {
            value = v;
        }
     
        public int get() {
            return value;
        }
     
        public static void main(String[] args) {
            // incorrect
            Foo[] fooList = new Foo[3];
            Foo f = new Foo();
            f.set(10);
            fooList[0] = f;
            f.set(20);
            fooList[1] = f;
            f.set(30);
            fooList[2] = f;
            print(fooList);
            // correct
            fooList = new Foo[3];
            Foo f1 = new Foo();
            f1.set(10);
            fooList[0] = f1;
            Foo f2 = new Foo();
            f2.set(20);
            fooList[1] = f2;
            Foo f3 = new Foo();
            f3.set(30);
            fooList[2] = f3;
            print(fooList);
        }
     
        public static void print(Foo[] arr) {
            for(Foo f : arr) {
                System.out.println(f.get());
            }
            System.out.println();
        }
    }

  6. The Following User Says Thank You to Junky For This Useful Post:

    Neo (April 18th, 2011)

  7. #5
    Junior Member Neo's Avatar
    Join Date
    Feb 2011
    Location
    Ohio
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: instantiating a object

    ok but suppose you don't know how many instances of Foo you need. say that fact is left up to the programs user. how would you allow for foo to be updated, also keeping in mind that the information being put in is unknown, other than what type of information you will be recieving.... i.e. string int double etc.....this is the part i'm stuck on. i could hard code in as many players as i deem necessary, but when the grader looks at the source and sees the program can only recieve say 5 players he will dock me points for having a falliable program. so i need to update player to an unknown number n and have it create each new player as n+1. and on top of that i have to be able to recall any of the players from the list at a given time and be able to remove that specific player from the list. these are the concepts i'm not finding in our book or in our notes.....

  8. #6
    Junior Member Neo's Avatar
    Join Date
    Feb 2011
    Location
    Ohio
    Posts
    14
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: instantiating a object

    and thank you both for taking the time, i always hate to ask others for help when i don't understand stuff. makes me feel dumb >.<
    Last edited by Neo; April 18th, 2011 at 11:15 PM.

  9. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: instantiating a object

    "ok but suppose you don't know how many instances of Foo you need. say that fact is left up to the programs user"

    You use a Collection to hold your objects as they can increase their size unlike an array. To stop getting user input you would use a flag. As soon as user enters the flag you stop looping.

    "also keeping in mind that the information being put in is unknown, other than what type of information you will be recieving.... i.e. string int double etc"

    This is impossible. You need to know what the input will be and in what format. Besides you seem to have it backwards. If your program needs to deal with people then you think about what characteristics a person has: name, address, date of birth etc. Then create a class to represent that data. Then your program prompts the user to input values to enable an object to be create. Adding some validation avoids you program crashing if user inputs junk. You don't do it the other way and try and create a class based on unknown input.

  10. #8
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: instantiating a object

    See the initial reply by helloworld.

    For more on ArrayLists, see the JavaDoc: ArrayList (Java Platform SE 6)
    Arraylist is a dynamic array/list, it will expand as it needs to.

Similar Threads

  1. instantiating class objects from an array
    By BadAnti in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 12th, 2011, 03:27 PM
  2. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  3. Instantiating jsp's during deployment time
    By tcstcs in forum JavaServer Pages: JSP & JSTL
    Replies: 1
    Last Post: April 1st, 2011, 02:35 PM
  4. Need help with instantiating a class
    By suxen in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 30th, 2011, 03:35 PM
  5. Instantiating a SuperClass Map from a SubClass
    By drexasaurus in forum Collections and Generics
    Replies: 1
    Last Post: September 9th, 2010, 10:51 PM