Home > Enterprise >  Arrays With Multiple Information Per Box
Arrays With Multiple Information Per Box

Time:11-09

So I have this practice question where I should create an array with the employee's information and pass it on to the class; there is a problem with my code which I cant seem to figure out.

What the code is meant to do is: Have the information as seen in the code put into an array, then passed to the methods in the class and then printed out to the user. (The code in the class is perfectly fine, hence why it's not included here).

If anyone could help, that'd be awesome.

Thank you.

// Code.

    // The Scanners.
    Scanner input = new Scanner(System.in);
    Scanner scan = new Scanner(System.in);
    
    // Taking Number Of Employees From The User.
    System.out.println("How many employees are there: ");
    int numberOfEmployees = input.nextInt();
    
    //Creating An Array With The Size Of Employees Entered By The User.
    Employee[] E = new Employee[numberOfEmployees];
    
    // Filling Out Information About Employees In Array.
    for(int i = 0; i <= E.length-1; i  ){
        System.out.println("Enter employee "   i   "'s name: ");
        String name = scan.nextLine();
        
        System.out.println("Enter employee "   i   "'s birth date: ");
        String bday = scan.nextLine();
        
        System.out.println("Enter employee "   i   "'s salary: ");
        double salary = input.nextDouble();
        
        System.out.println("Enter employee "   i   "'s overtime: ");
        int overtime = input.nextInt();
        
        E[i] = (name, bday, salary, overtime);
    }
    
    System.out.println("Employee's Information"
              "\n----------------------"
              "\n----------------------");
    
    for(int i = 0; i <= E.length-1; i  ){
        E[i].print();
    }
}

CodePudding user response:

There are two issues here that need to be addressed.

First, don't create more than one Scanner object, reading from System.in. I know you did this, because of this issue (check it out): Scanner is skipping nextLine() after using next() or nextFoo()? - But there is another solution than creating another Scanner.

The second issue is here:

E[i] = (name, bday, salary, overtime);

You have a Employee[] which you are trying to fill. But you got the syntax wrong. You actually want to create a new Employee(...) to fill your array with.

So this line should correctly be (provided that Employee has a constructor with the given types):

E[i] = new Employee(name, bday, salary, overtime);

When considering this in your code snippet:

Scanner scan = new Scanner(System.in);
// Taking Number Of Employees From The User.
System.out.println("How many employees are there: ");
int numberOfEmployees = scan.nextInt();
scan.nextLine(); // <- reads the newline from the console

//Creating An Array With The Size Of Employees Entered By The User.
Employee[] E = new Employee[numberOfEmployees];

// Filling Out Information About Employees In Array.
for(int i = 0; i <= E.length-1; i  ){
    System.out.println("Enter employee "   i   "'s name: ");
    String name = scan.nextLine();
    
    System.out.println("Enter employee "   i   "'s birth date: ");
    String bday = scan.nextLine();
    
    System.out.println("Enter employee "   i   "'s salary: ");
    double salary = scan.nextDouble();
    
    System.out.println("Enter employee "   i   "'s overtime: ");
    int overtime = scan.nextInt();
    
    // create a new employee with the entered information and save in the array
    E[i] = new Employee(name, bday, salary, overtime);
}

CodePudding user response:

I am not sure with the Double scanner.

The obvious one is since you defined an empty array of Employee object type, the array can only be fit by Employee objects.

Employee[] E = new Employee[numberOfEmployees]

to define one object, you need to allocate a memory onto it, like:

new Employee(name, bday, salary, overtime);

then assign it into the array E

E[i] = new Employee(name, bday, salary, overtime);

i hope that clear things up.

  • Related