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

Thread: User creates objects. How to?

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default User creates objects. How to?

    Hey all!

    I have a question regarding how a user can generate a new object.

    For example, lets say that I have a GUI application in which the user will be able to put an order. That order has to be created, with a new name and new values. The order itself will be stored in an array or some other type of collection.

    My question is how do you put it in code in the most simplest way? The area I'm stuck on is generating new object names, and these must be different (hence different orders).

    Here is an example without GUI:

    // Using scanner class
    		int a = 0;
    		System.out.println("Hey! Place a new order!");
    		a = scan.nextInt();
     
    		if(a>0){
    			Order x = new Order();
    			x.setOrderID(a);
     
                    CollectionOfOrders.put(x);
     
    		}

    Can you point me towards the right direction: for-loop? It's the names
    Last edited by Sputnik; December 1st, 2010 at 06:24 AM.


  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: User creates objects. How to?

    What do you mean by "object name"? What do you mean by "simplest way"?

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: User creates objects. How to?

    Hey Kevin!

    What I mean is that an instance of a class is being created everytime that above piece of code is repeated. But the name of the instance created remains the same - especially if I want to put it in a container of some sort. What happens if that piece of code is repeated, and I try to add the same instance name to the container - no go.


    How do I make sure that everytime that instance is being created has a unique identifier (i.e. name).

    Order o = new Order();

    The next Order created can't be called "o" again, can't it, so how do I make sure otherwise?

  4. #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: User creates objects. How to?

    Why can't it be called "o" again? What happened when you tried your original code?

  5. #5
    Member
    Join Date
    Jul 2010
    Location
    Washington, USA
    Posts
    307
    Thanks
    16
    Thanked 43 Times in 39 Posts

    Default Re: User creates objects. How to?

    You can use an ArrayList of objects.


    ArrayList<order> orderArrayList = new ArrayList<order>(); //Makes an arraylist to hold all of the order objects
     
    for (int i=0; i < 10; i++)
          orderArrayList.add(new order());  //Makes a new orderobject and adds it to the list

  6. #6
    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: User creates objects. How to?

    Quote Originally Posted by Brt93yoda View Post
    You can use an ArrayList of objects.
    That's what the OP is doing. From the original post:
    CollectionOfOrders.put(x);

Similar Threads

  1. How do I let the user choose how big an array is...
    By Roon Hapoon in forum Collections and Generics
    Replies: 2
    Last Post: October 5th, 2010, 12:15 AM
  2. Netbeans User Manual
    By systech44 in forum Java IDEs
    Replies: 1
    Last Post: December 8th, 2009, 12:56 PM
  3. User Defined Methods
    By mgutierrez19 in forum Object Oriented Programming
    Replies: 11
    Last Post: October 20th, 2009, 06:57 PM
  4. Implementing LinkedList as a user?
    By vluong in forum Collections and Generics
    Replies: 3
    Last Post: October 15th, 2009, 03:00 AM
  5. how to know user pressed a key in the keyboard
    By cilang in forum File I/O & Other I/O Streams
    Replies: 16
    Last Post: September 11th, 2009, 10:08 AM