0

I have an array contains Employee object.

How do I print every element of the array?

I can only get it to print the last input.

/*This is Employees class
  toString accepts lastname, firstname, payrate, workhour, grosspay, tax, netpay, and return a string */
public void display(Employee a[])
    {
        for (int i=0; i<max; i++)
        {
            System.out.println(a[i].toString());
        }
     }

// main
        for (int a=0; a<max;a++)
        {
            list[a]=emps.getinfo(emp);
        }
        emps.display(list);

//Employee class
//  I am assuming there is something wrong with these two methods in my Employee class. 
public Employee(Employee e)
        {
            lastname=e.lastname;
            firstname=e.firstname;
        }
// Argument will be lastname, firstname, workhour, payrate, grosspay, tax and net.
public String toString()
        {
            return String.format("format", argument);
        }

The following link is the complete code. https://imgur.com/gallery/bTXPSKb

Input

qwe , ewq 5 5
rtw , gtr 7 7

Output

rtw ,gtr 7.00 7.00 49.00 7.35 41.65 
rtw ,gtr 7.00 7.00 49.00 7.35 41.65 

Expecting

qwe ,ewq 5.00 5.00 25.00 3.75 21.25
rtw ,gtr 7.00 7.00 49.00 7.35 41.65 
7
  • Where do you set the properties of the Employee instances? Commented Apr 7, 2019 at 7:53
  • In the employees class. Commented Apr 7, 2019 at 7:57
  • I don't see it in the code you posted. Also, please post the Employee class. Commented Apr 7, 2019 at 7:59
  • I post the Employee class. As for the properties of the Employee instances, are you asking for the constructors of Employee? Commented Apr 7, 2019 at 8:08
  • Please post complete code. Including the instance variables of Employee class, including the actual body of getInfo. Commented Apr 7, 2019 at 8:13

2 Answers 2

1

The answer to your question regardless of code context:

Integer[] integers = {1, 2, 3};
System.out.println(Arrays.toString(integers));

looks like problem in

list[a]=emps.getinfo(emp);

most likely emp remains the same during your loop, so you get the same result. Your link does not provide any info, unfortunately.

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

Comments

1

I would suggest you write your array as List for more generic use of the object. it's a better practice this way, and now you can rock-n-roll with it like this:

List<String> items = new ArrayList<>();
items.add("A");
items.add("B");
items.add("C");
items.add("D");
items.add("E");

//lambda
//Output : A,B,C,D,E
items.forEach(item->System.out.println(item));

//Output : C
items.forEach(item->{
    if("C".equals(item)){
        System.out.println(item);
    }
});

//method reference
//Output : A,B,C,D,E
items.forEach(System.out::println);

//Stream and filter
//Output : B
items.stream()
    .filter(s->s.contains("B"))
    .forEach(System.out::println);

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.