cae52
cae52

Reputation: 19

Creating new objects in the body of a loop

I am trying to write a very basic java program that will run allow the user to enter information about some employees. What I have in mind is a for loop that will create a separate Employee object (of an Employee class, already defined) and then retain each object in memory after the entire loop has run. Something like this:

      String input = JOptionPane.showInputDialog("How many Employees are we "
         + "entering into the system?");
      int numEmployees = Integer.parseInt(input);

    for (int count = 1; count <= numEmployees; count ++)
    {
    Employee employee(count) = new Employee();
    }

I know this code doesn't work; but I hope it makes clear that what I am trying to do is: each iteration of the loop create a new object (employee1, employee2....employeeN) and then have some code that will allow the user to enter the needed data.I already have all the necessary accessor and mutator methods. The object would remain in memory after the loop is finished running. Any ideas? Thanks everyone!

Upvotes: 0

Views: 995

Answers (7)

Jan Nielsen
Jan Nielsen

Reputation: 11799

import java.util.*;

public class EmployeeService
{
    public static List<Employee> createEmployees(int numberOfEmployees)
    {
        List<Employee> employees = new ArrayList<Employee>();

        for(int i=0; i<numberOfEmployees;i++)
        {
            Employee e = newEmployee();

            // gather required employee information

            employees.add(e);
        }

        return employees;
    }

    /**
     * Create an in-memory employee object.
     *
     * @return  in-memory employee object
     **/
    public static Employee newEmployee()
    {
        return new Employee();
    }
}

class Employee
{
}

Upvotes: 0

indivisible
indivisible

Reputation: 5012

I think this is what you were getting to. Make an empty array of Employees and then assign then in turn inside the loop. You could alose opt for an ArrayList as in the other answer.

String input = JOptionPane.showInputDialog("How many Employees?");
int numEmployees = Integer.parseInt(input);

Employee[] employess = new Employee[numEmployees];
for (int count = 0; count < numEmployees; count ++)
{
    employees[count] = new Employee();
}

Upvotes: 0

user_loser
user_loser

Reputation: 871

You could use an java.util.ArrayList to store the Employee objects. Your code may look something like this:

package EmployeeStuff; 

import java.util.ArrayList;

// rest of the class code 

ArrayList<Employee> emps = new ArrayList<Employee>();


String input = JOptionPane.showInputDialog("How many Employees are we "
     + "entering into the system?");
  int numEmployees = Integer.parseInt(input);

for (int count = 1; count <= numEmployees; count ++)
{
    emps.add(new Employee()); // add an employee object to the ArrayList 
}

:D

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533530

Sounds like you want to use an array

Employee[] employees = new Employee[numEmployees];
for(int i = 0; i < numEmployees; i++)
    employees[i] = new Employee();

Upvotes: 2

Marco Acierno
Marco Acierno

Reputation: 14847

You could use an array (in this case is good, because you already know how much employees you will have) or an ArrayList (dynamic size, better if you don't know how much elements it will contains).

Employee[] employees = new Employee[numEmployees];

Then every cycle you should do

for (int count = 0; count < numEmployees; count ++)
{
    employees[count] = new Employee();
}

To create a new Employee and store it. Anyway i edited some parts of your code.

count = 1 will be count = 0 Because arrays starts from 0

count <= numEmployees will be count < numEmployees Since starts from 0, <= will go to numEmployees and arrays size is numEmployees-1 it means "out of bounds" of the array.

Upvotes: 0

kabb
kabb

Reputation: 2502

You seem to be trying to create variable variable names, which isn't possible in Java. Instead, you should store your employees in an array.

Employee[] employees = new Employee[numEmployees];

To add employees to the array, you can do this

employees[count] = new Employee();

Upvotes: 0

RMachnik
RMachnik

Reputation: 3684

Try storing it in some collection.

     List<Employee> employeList = new ArrayList<Employee>();

     for (int count = 1; count <= numEmployees; count ++)
     {
        employeList.add(new Employee());
     }

or simply in array.

         Employee[] employeList = new Employee[numEmployees];

         for (int count = 1; count <= numEmployees; count ++)
         {
            employeList[i]=new Employee();
         }

Upvotes: 0

Related Questions