0

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();
    }
}
  • "_...there is a problem with my code which I cant seem to figure out._" - What's the problem? What is happening when you run your code? What are your inputs and outputs (and expected outputs)? Are you getting any exceptions? This is the information that should be contained in your question. – maloomeister Nov 09 '21 at 07:58
  • Also, why are you creating two `Scanner` objects? – maloomeister Nov 09 '21 at 07:58
  • When I'm using one scanner and taking input of multiple strings my code runs the breaks halfway through. Also, no I'm not getting expectations it says the variables have been defined already & Employee is not a functional interface. – Tareq El-Sayyed Nov 09 '21 at 08:01
  • Yes, check https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo for the reason and fix for the scanner breaking halfway through. – maloomeister Nov 09 '21 at 08:02
  • Thank you!! But do you know how I could fix the other problem? – Tareq El-Sayyed Nov 09 '21 at 08:03

2 Answers2

0

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);
}
maloomeister
  • 2,461
  • 1
  • 12
  • 21
-1

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.

DoubleLiu
  • 52
  • 1
  • 6