Java toString方法 - 无法正确打印

时间:2014-04-18 07:35:08

标签: java string return output tostring

我试图从类Employee获取toString,但它所做的就是给我一个[]的输出。我得到了toString方法的输入,但是有关如何将它输出到输出的任何想法?

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

另一类是:

 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

4 个答案:

答案 0 :(得分:3)

您正在调用toString的{​​{1}}方法,而非任何ArrayList。实际上你还没有创建该类的实例。尝试:

Employee

答案 1 :(得分:1)

修改

<强>首先

好吧,你需要至少制作一些Employee对象并将它们添加到你的列表中;)否则,就没有&#34;没有&#34; (打印到&#34;没有&#34;)。因此,在您从用户的输入(ID等)中读取所有内容之后,请从中创建一个新的Employee:

// 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);

现在列表中有一名员工可以打印。您可以通过询问其大小来测试列表中是否有某些内容:

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

<强>第二

您不能在您正确想要打印的员工类上致电toString()。您可以在员工列表中将其调用。因此,您将看到ArrayList类的toString()方法的结果(这不是您所期望的,但它是正确的)。相反,遍历列表并打印每个员工。请注意,将自动调用toString()方法,因为System.out.println会将您的对象转换为字符串(实际上意味着调用此方法)。

试试这个:

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

答案 2 :(得分:1)

您的employeeInfo对象似乎为空,因此您打印的是一个空列表。

确保在打印前将值放入其中。

请注意, toString()的{​​{1}}已实现为递归调用其对象的ArrayList ,因此可以使用toString()打印整个ArrayList.toString() - 如果这是你想要的。无需迭代所有元素。

答案 3 :(得分:0)

您正在打印数组的toString。 employeeInfo是Employees的ArrayList。 迭代和打印员工,如:

for (Employee e : employeeInfo)
{
    System.out.println(e.toString());
}