Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Default For loop to create objects

    Ok so I am making an ArrayList of objects and I need to be able to generate as many objects (Savings Accounts in this case) as the user inputs. So I'm guessing I need a for loop to generate objects. I don't really know how to do this. If someone could just help me out with the algorithm that would be awesome!

    Thanks

  2. #2
    collin389 is offline Senior Member
    Join Date
    Nov 2009
    Posts
    235
    Rep Power
    6

    Default

    You would have a for loop that checks if the user wants an other input. If they do, then make an object, add it to an arraylist, and you go back to the top of the for loop.
    When the user inputs the values for the object, store them to temp values, and call arraylist.add(new SAccount(temp1, temp2, temp3, ..., tempn));

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    If the class already has setters for those values then there is no need to store the values in temp variables. Just create the account object and set the values onto it.

  4. #4
    Join Date
    Mar 2009
    Posts
    31
    Rep Power
    0

    Default

    When I add these objects to the ArrayList the temp values that I input do not get stored into the objects. here is my code for the method to create the objects

    Java Code:
    public void createAccountObjects(int numberOfAccounts){
        
    
            if (numberOfAccounts <= 25 && numberOfAccounts != 0){
    
                for( int i = 0; i<numberOfAccounts;i++ ){
    
                    System.out.println("Enter the balance for the new account: ");
                    double newBalance = input.nextDouble();
                    System.out.println("Enter the interest rate for new account: ");
                    double newInterestRate = input.nextDouble();
                    listOfAccounts.add(i, new SavingsAccount(newBalance,newInterestRate));
                    
                    
                }
                System.out.println(listOfAccounts);
    
            }
        else
            System.out.println("You cannot create more than 25 accounts");

  5. #5
    bit_bit is offline Member
    Join Date
    Feb 2010
    Location
    Italy
    Posts
    4
    Rep Power
    0

    Default

    ok, first of all, i don't really understand why you have put the if condition before the loop (usually an array list should be "infinite", you can put in it as many objects as the memory can contain and to insert an object there is no need for at least another object to be in the list).
    anyway, assuming that you have done that because you needed to build it that way, all you have to do is create a cicle where the user enters the input for the new object to insert in the list or enters the "exit" command.
    for example:
    Java Code:
    public void createAccountObjects(){
      System.out.println("choose the option you desire:");
      System.out.println("1 - create new account.");
      System.out.println("2 - exit.");
      System.out.println("3 - print this menu.");
    
      do{
        System.out.println("choose the option you desire:");
    
        if(input == 1){
          // create new account and insert it into the list.
        }
        else if(input == 2){
          // exit the loop.
        }
        else if(input == 3){
          // print the menu once again.
        }
        else{
          // print something that declares an error in input.
        }
      } while(input != 2);
    }
    i don't know if it was what you were looking for.
    hope i was helpful.

    all the best.
    bit

Similar Threads

  1. How to create an array of objects
    By redmaverick in forum New To Java
    Replies: 7
    Last Post: 10-19-2009, 03:14 AM
  2. while-loop stopping on first loop
    By davester in forum New To Java
    Replies: 6
    Last Post: 06-26-2009, 09:46 PM
  3. Replies: 1
    Last Post: 01-22-2009, 05:25 PM
  4. Replies: 2
    Last Post: 11-21-2008, 06:59 PM
  5. How do I create an array with every cycle of a loop?
    By blackhole8746 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:49 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •