2

I have the following code. I need to store EmployeeNumber and Name into an Array. I'm not sure what I am doing wrong:

for(int i = 0; i<5; i++)
{   
    int numEmps = 0;

    System.out.print("Enter Employee number: ");
    employeeNumber = sc.nextLine();
    numEmps++;
    System.out.print("Enter Employee name: ");
    name = sc.nextLine();
    numEmps++;

    employees[numEmps] = new Employee(employeeNumber, name, role, level);
}
1
  • how Employee look like? Commented May 19, 2017 at 9:29

5 Answers 5

2

First You have to use int numEmps = 0; outside your loop :

int numEmps = 0;
for(int i = 0; i<5; i++){
  ...
}

In your case you put all employees in the employees[2]

Second you have to use only one numEmps++;


Your code should look like :

int numEmps = 0;//<<------------------------------------------Problem 1
for (int i = 0; i < 5; i++) {
    System.out.print("Enter Employee number: ");
    employeeNumber = sc.nextLine();
    //numEmps++;<<--------------------------------------------Problem 2
    System.out.print("Enter Employee name: ");
    name = sc.nextLine();
    numEmps++;

    employees[numEmps] = new Employee(employeeNumber, name, role, level);
    //          ^^------------------You can avoid to use numEmps, instead you can use i
}

Note like @Le Duy Khanh mention in comment, you ca use employees[i] instead of using a new variable

Sign up to request clarification or add additional context in comments.

4 Comments

why don't just use employees[i]? then no need numEmps++;
@LeDuyKhanh we don't see the other part of code so we don't know what the OP want to do with numEmps but your suggest is also correct i will edit my answer
then your ans should be numEmps = [some_value_to_start]
and what i said in the first problem @LeDuyKhanh did you check my answer?
0

Here you store these two information in an array of Employee.
It seems fine.
Before the loop, you should declare the array :

Employee[] employees = new Employee[5];

Besides in the loop you don't need to use numEmp (you increment twice by the way, which is not suitable) as you have already the int i value that you declare in the loop. So, just use i as index of the element added in the array.

And this should be enough :

employees[i] = new Employee(employeeNumber, name);

as you don't specify the value of role and level in your requirement and in your code.

Ideally, employeeNumber = sc.nextLine(); should be converted to a numeric value(int number = Integer.valueOf(employeeNumber)) if you want to represent employeeNumber as a number (for example an int or a long).

Comments

0

since you are increasing the numEmps variable twice on every loop iteration then your array is getting filled every 2 elements...

Comments

0

Put int numEmps = 0; out of your for loop.

This will be re-initialized to zero every time looped.

Comments

0
int numEmps = 0;

for(int i = 0; i<5; i++)
{
   System.out.print("Enter Employee number: ");

   employeeNumber = sc.nextLine();

   numEmps++;
   System.out.print("Enter Employee name: ");

   name = sc.nextLine();

   numEmps++;

   employees[numEmps] = new Employee(employeeNumber, name, role, level);
}

Use your variable numEmps outside the for loop, otherwise in every iteration it is becoming 0 again.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.