Question

I'm trying to get the toString from the class Employee, but all it does is give me an output of [ ]. I got the input for the toString method, but any ideas on how to get it to carry out to the output?

public class A5
{ // begin class

public static void main(String[] args) 
    { // begin main

    // ********** CREATE OBJECTS **********

        ArrayList<Employee> employeeInfo = new ArrayList<Employee>();

     // ********** GET INPUT **********

        // get the employee's ID
        System.out.println("\nEnter your employee ID.");
        ID = keyboard.nextInt(); //get the input and set it to the local varaible ID
        //System.out.println("Employee ID: " + ID);

        // get the employee's hours worked
        System.out.println("\nEnter the amount of hours you worked this week.");
        Hours = keyboard.nextInt(); //get the input and set it to the local varaible HoursWorked
        //System.out.println("Hours worked: " + Hours);

        // get the employee's wage
        System.out.println("\nEnter your wage.");
        Wage = keyboard.nextDouble(); //get the input and set it to the local varaible Wage
        //System.out.println("Employee wage: " + Wage); 

    // ********** OUTPUT **********

        System.out.println(employeeInfo.toString());

    // ********** CLOSING MESSAGE **********

        System.out.println("\nEnd of Processing!");

} // end main
} // end class

And the other class is:

 public class Employee
{  // begin class

    private int ID;                     // employee's id
    private int Hours;                  // number of hours worked
    private double Wage;                // pay per hour

  public Employee(int IDnumber)
    {
        ID = IDnumber;
    }

    public int getID()
    {
        return ID;
    }

    public void setWage(double HourlyWage)
    {
        Wage = HourlyWage;
    }

    public double getWage()
    {
        return Wage;
    }

    public void setHours(int hoursWorked)
    {
        Hours = hoursWorked;
    }

    public double getHours()
    {
        return Hours;
    }

    public String toString()    // overrides the toString method inherited from object
    { // begin toString
        String strout = "\nId \t\t Hours \t\t Rate \t\t Regular Pay \t Overtime Pay \t Gross Pay\n";
        strout += ID + "\t " + Hours + "\t\t\t $" + (df1.format(Wage)));        

        return strout;
    } // end toString

 } // end class
Was it helpful?

Solution 2

EDIT:

First:

OK, well, you need atleast to make some Employee objects and add them to your list ;) Otherwise, there is "nothing" (which prints out to "nothing"). So after you read all your stuff from the user's input (ID and so) make a new Employee out of it:

// make a new employee
Employee employee = new Employee(id);  // pass your id
employee.setWage(...);  // pass your wage
...  // pass other things

// add it to the list of course
employeeInfo.add(employee);

Now there is an employee in the list which you can print. You can test if something is on the list by asking for its size:

System.out.println(employeeInfo.size());

Second:

You don't call toString() on your employee class, which you properly want to print. You call it on your list of employees. Therefor you will see the result of the toString() method of the ArrayList class (which is not what you expect, but what is correct). Instead, iterate over the list and print every employee. Note that the toString() method will be called automatically, since System.out.println will convert your object to a string (which actually means to call this method).

Try this:

for(Employee employee: employeeInfo)
    System.out.println(employee);

OTHER TIPS

You are calling the toString method of the ArrayList, not of any Employee. In fact you did not yet create an instance of that class. Try:

System.out.println(new Employee());

Your employeeInfo object seems to be empty, and thus you are printing an empty list.

Make sure you put values in it before printing.

Note that the toString() of the ArrayList is implemented to recursively invoke toString() of its object, so it is fine to use ArrayList.toString() to print the entire ArrayList - if that's what you want. No need to iterate for all elements.

You are printing the toString of an Array. employeeInfo is an ArrayList of Employees. Iterate and print employees like:

for (Employee e : employeeInfo)
{
    System.out.println(e.toString());
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top