2014-04-18 37 views
0

我试图从Employee类中获取toString,但它所做的只是给出了[]的输出。我得到了toString方法的输入,但是有关如何让它执行到输出的想法?Java 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 
+0

你正在做的是调用'ArrayList'对象的'toString'方法。你需要调用'Employee'对象的'toString'方法。 –

+0

看起来你的'employeeInfo'对象是空的,你能验证它吗? – amit

回答

1

编辑:

第一:

OK,好,你需要至少在做出一些Employee对象,并添加他们到你的名单;)否则,有“没有”(打印出“没有”)。你看从用户的输入您的所有东西(ID等),所以之后作出新的员工出来的:

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

它什么都不会做。 employeeInfo似乎是空的,否则他不会得到'[]',但'[string1,string2,...]' – amit

+0

哈哈,我编辑了我的答案。 –

+0

非常感谢!现在它工作了! :) – 1CoolLittleChick

3

要调用的又没EmployeeArrayListtoString方法。实际上,您还没有创建该类的实例。请尝试:

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

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

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

它什么都不会做。 employeeInfo似乎是空的,否则他不会得到'[]',但'[string1,string2,...]' – amit

+0

下面有一条评论说//这里有代码... –

+0

它不会物。该列表是空的,因为输出是'[]'。 'ArrayList'递归调用其元素的'toString()',如果'someArrayList'为空,那么获得'someArrayList.toString()'打印'[]'的唯一方法就是。 – amit

1

employeeInfo对象似乎是空,这样的话你要打印一个空列表。

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

注意的ArrayListtoString()实现递归调用它的对象的toString(),所以这是很好用ArrayList.toString()打印整个ArrayList - 如果这就是你想要的。无需迭代所有元素。