【问题标题】:Java toString Method - Not printing correctlyJava toString 方法 - 无法正确打印
【发布时间】:2014-06-02 16:42:33
【问题描述】:

我试图从 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

【问题讨论】:

  • 你正在做的是调用ArrayList对象的toString方法。需要调用Employee对象的toString方法。
  • 您的employeeInfo 对象好像是空的,您能验证一下吗?

标签: java string return output tostring


【解决方案1】:

您正在调用ArrayList 的toString 方法,而不是任何Employee 的方法。事实上,您还没有创建该类的实例。试试:

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

【讨论】:

    【解决方案2】:

    编辑:

    第一:

    好的,好吧,您至少需要创建一些 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);
    

    【讨论】:

    • 它什么也不做。 employeeInfo 好像是空的,否则他不会得到[],而是[string1, string2, ... ]
    【解决方案3】:

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

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

    注意ArrayList的toString()实现递归调用其对象的toString(),所以使用ArrayList.toString()打印整个ArrayList就可以了-如果那是你想要的。无需对所有元素进行迭代。

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      • 它什么也不做。 employeeInfo 好像是空的,否则他不会得到[],而是[string1, string2, ... ]
      • 没关系。该列表为空,因为输出为 []。 ArrayList 递归调用其元素的 toString(),获得 someArrayList.toString() 以打印 [] 的唯一方法是如果 someArrayList 为空。
      猜你喜欢
      • 1970-01-01
      • 2014-05-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多