[英]Java toString Method - Not printing correctly
我正在尝试从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
。 实际上,您尚未创建该类的实例。 尝试:
System.out.println(new Employee());
编辑:
第一:
OK,好,您需要至少创建一些Employee对象并将其添加到列表中;)否则,将没有任何内容(打印输出为“ nothing”)。 因此,在您从用户输入(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);
您的employeeInfo
对象似乎为空 ,因此您正在打印一个空列表。
确保在打印之前在其中输入值。
请注意, 实现ArrayList
的toString()
来递归调用其对象的toString()
,因此可以使用ArrayList.toString()
打印整个ArrayList
如果您要这样做。 无需迭代所有元素。
您正在打印数组的toString。 employeeInfo
是Employees的ArrayList。 迭代并打印员工,例如:
for (Employee e : employeeInfo)
{
System.out.println(e.toString());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.