2

I have been facing problems in passing array in java. The code is:

package input_output;

import static java.lang.System.out;
import java.util.Scanner;

public class InputOutput {
    private static Scanner sc;
    public static void main(String []args){
        sc = new Scanner(System.in);
        out.print("Enter the length of arrays   :\t");
        int n = sc.nextInt();
        Employee[] emp = new Employee[n];
        for(int i=0;i<n;i++){
            out.print("\nEnter name and age of " + (i+1) + " employee   :\t");
            emp[i] = new Employee();
            emp[i].setName(sc.nextLine());
            sc.nextLine();
            emp[i].setAge(sc.nextInt());
        }

        Operation operate = new Operation(emp,n);

        operate.printOnScreen();

    }
}

class Operation{ 
    Employee []emp;
    public Operation(Employee[] emp,int n){
        this.emp=emp;
        for(Employee e: this.emp)
          e = new Employee();
    }

    public void printOnScreen() { 
        for(Employee e : emp){
            e = new Employee();
            out.println("Name:\t" + e.getName() + "\t\tAge:\t" + e.getAge() + "\n");
        }
    }
}

Employee class is a simple bean containing getter and setter methods of age and name.

The output is coming :

Name: null
Age:0

What's my mistake?


Now I have changed the constructor to

public Operation(Employee[] emp, int n){
        this.emp=emp;
}

and removed the line:

e = new Employee(); 

from PrintOnScreen()

And the output is:

Name:
Age:21

3
  • What are you trying to achieve with both for(Employee e: this.emp) e = new Employee();? Commented Sep 5, 2014 at 11:08
  • Please refrain from changing the question so that it renders valid answers invalid. Changing the source code with the corrected version is not right. Commented Sep 5, 2014 at 11:17
  • How about also posting the code for the Employee class so we can see whether the getter/setter is correctly implemented? Commented Sep 5, 2014 at 11:32

4 Answers 4

4

You're not outputting data from the array in the following code:

public void printOnScreen() 
{ 
    for(Employee e : emp)
    {
        e = new Employee(); // <<< THIS IS WRONG, AS e IS ALREADY SET BY LOOP!!
        out.println("Name:\t" + e.getName() + "\t\tAge:\t" + e.getAge() + "\n");
    }
}

This code creates a new e on every iteration of the loop => you're not outputting the entry from the array, but the freshly created one. The code should read

public void printOnScreen() 
{ 
    for(Employee e : emp)
    {
        out.println("Name:\t" + e.getName() + "\t\tAge:\t" + e.getAge() + "\n");
    }
}

Also, the following code doesn't make sense:

public Operation(Employee[] emp,int n){
    this.emp=emp;
    for(Employee e: this.emp)
      e = new Employee();
}

Why do you iterate over all the employees in this.emp to create new instances of Employee that are stored nowhere? Also, the n parameter is not used. This code should read:

public Operation(Employee[] emp)
{
    this.emp=emp;
}
Sign up to request clarification or add additional context in comments.

2 Comments

i have done changes as u have said,, but the o/p is now: Name: Age:21 that means name is empty
Well, as you didn't show us the implementation of Employee, it is impossible for us to tell where the problem is. My best guess is that getter and setter simply don't work.
3

To your second problem: (empty name fields)

The problem is the sc.nextInt() call. This reads an integer from the input stream but leaves the carriage return and line feed signs there. The next sc.nextLine() call will read these sign and nothing else. That way all names are filled with \r\n.

You can fix that in to ways:

public static void main(String []args){
    sc = new Scanner(System.in);
    out.print("Enter the length of arrays   :\t");
    int n = sc.nextInt();
    sc.nextLine(); // <-- new
    Employee[] emp = new Employee[n];
    for(int i=0;i<n;i++){
        out.print("\nEnter name and age of " + (i+1) + " employee   :\t");
        emp[i] = new Employee();
        emp[i].setName(sc.nextLine());
        //sc.nextLine(); // <-- removed 
        emp[i].setAge(sc.nextInt());
        sc.nextLine(); // <-- new
    }
    //..
}

Or

public static void main(String []args){
    sc = new Scanner(System.in);
    out.print("Enter the length of arrays   :\t");
    int n = Integer.parseInt(sc.nextLine()); // <-- changed
    Employee[] emp = new Employee[n];
    for(int i=0;i<n;i++){
        out.print("\nEnter name and age of " + (i+1) + " employee   :\t");
        emp[i] = new Employee();
        emp[i].setName(sc.nextLine());
        //sc.nextLine(); // <-- removed 
        emp[i].setAge(Integer.parseInt(sc.nextLine())); // <-- changed
    }
    //..
}

If this solves your second problem, please accept Thorsten Dittmars anwser for you main question.

Comments

1

While printing data, you are printing data of new employee and not the earlier employee object that you created.

Comments

0

Not sure, but I think Array is not copying content with "=" in Java. Change in class Operation

this.emp=emp;
for(Employee e: this.emp)
    e = new Employee();

to

for(int i=0; i<n; i++) {
    this.emp[i].setName(emp.getName());
    this.emp[i].setAge(emp.getAge());
}

Then change

operate.printOnScreen();

to

operate.printOnScreen(emp);

and

public void printOnScreen() { 
    for(Employee e : emp){
        e = new Employee();
        out.println("Name:\t" + e.getName() + "\t\tAge:\t" + e.getAge() + "\n");
    }
}

to

public void printOnScreen(Employee emp) { 
    for(Employee e : emp){
        out.println("Name:\t" + e.getName() + "\t\tAge:\t" + e.getAge() + "\n");
    }
}

3 Comments

The problem has nothing to do with array copying.
this.emp=emp; would copy the contents?
Heave you heard of references?

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.