0

I have an assignment where I have to have a class named Employee with two variables and a class called Salary that inherits those variables and declares new ones. Within the Salary class, I have to create a method that will take user input for the different variables. The first number inputted is the number of employees you are giving information for and tells the loop how many times to go around. After typing in the information for all the employees the information must be presented like this

123 John engineer 40000.0
124 Emma Testing 33000.0

The only other thing that is required in the program is a function that will check the salary variable and if it is under 20000, not display the employee information to the console.

I have the user input working but I can't seem to get the array to display properly. Whatever I try I either get gibberish or null. I have tried putting the array and the array printing function in the display method and the main method and it doesn't make a difference. How can I fix this?

Side note: I know having different scanners for each input is not the best way of doing things it's just temporary. I'm trying to get the program to work before cleaning it up. The reasoning for it is a type mismatch error using one scanner as a result of scanning an int, then two strings, then a double.

This is the code I have right now:

import java.util.Scanner;
class Employee {
     int employee_id;
     String employee_name;
     Employee(int employee_id, String employee_name) {
     }
     Employee() {
     }
     }
class Salary extends Employee {
     double monthly_salary;
     String designation;
     int num_of_employees;
     int n = 0;
     Salary(int employee_id, String employee_name, String designation, double monthly_salary) {
         super(employee_id, employee_name);
     }
     Salary() { 
     }
     void display() { 
         Scanner s = new Scanner(System.in);
         Scanner c = new Scanner(System.in);
         Scanner a = new Scanner(System.in);
         Scanner e = new Scanner(System.in);
         Scanner t = new Scanner(System.in);
         num_of_employees = s.nextInt();
         for (;n<num_of_employees;) {
         employee_id = c.nextInt();
         employee_name = a.nextLine();
         designation = e.nextLine();
         monthly_salary = t.nextDouble();
         n++;
         }
         s.close();
         c.close();
         a.close();
         e.close();
         t.close();
     }
}
public class Solution2 {
     public static void main(String[] args) {
     Salary sal = new Salary();
     Salary salary[] = new Salary[10];
     sal.display();
     salary[sal.n] = new Salary(sal.employee_id, sal.employee_name, sal.designation, 
     sal.monthly_salary);
     if (sal.monthly_salary >= 20000) {
         System.out.println(salary[sal.n]);
     }
     else {
         System.out.println("");
     }
  } 
}

The result from the array with this code is

Salary@7b23ec81
  • 1
    That linked question doesn't accurately reflect your issue. This is only partly an issue with printing an array. What you want to do is "print" an object. You can do that by overridding `toString`. See this question: https://stackoverflow.com/questions/10734106/how-to-override-tostring-properly-in-java – Christopher Schneider Mar 07 '20 at 17:10
  • I have tried to do this by using Arrays.toString(salary) as well as creating a new string, setting it equal to Arrays.toString(salary), and displaying that. According to forums, these are the only ways to override toString for Arrays. When I do either of those I get [null, null, null, null, null, null, null, null, null, null] as my output. – George Seror IV Mar 07 '20 at 17:33
  • You don't override toString on Arrays. You would need to utilize toString on every object inside the array. If you are seeing null, then the objects inside your array are null. – Christopher Schneider Mar 07 '20 at 19:52

0 Answers0