Java 代码的格式化问题

Formatting Issues with Java Code

我在格式化字符串时遇到问题,因为我不断收到 MissingFormatArgumentException 错误:

Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%s'
    at java.util.Formatter.format(Unknown Source)
    at java.util.Formatter.format(Unknown Source)
    at java.lang.String.format(Unknown Source)
    at Employee.toString(Employee.java:36)
    at CommissionEmployee.toString(CommissionEmployee.java:112)
    at CommissionEmployeeTest.main(CommissionEmployeeTest.java:20)

这是我的代码

员工Java 超类文件 public class 员工 {

    private final String firstName;
    private final String lastName;
    private final String socialSecurityNumber;


    //SuperClass Argument Constructor
    public Employee(String firstName, String lastName, String socialSecurityNumber)
    {

        this.firstName = firstName;
        this.lastName = lastName;
        this.socialSecurityNumber = socialSecurityNumber;
    }

        //return first name
        public String getFirstName()
        {
            return firstName;
        }

        //return last name
        public String getLastName()
        {
            return lastName;
        }

        //get Social Security number
        public String getSocialSecurityNumber(){
            return socialSecurityNumber;
        }

        public String toString(){
            return String.format("%s: %s %s%n%s: %s%n%s:", "commission employee",getFirstName(), getLastName(), "social security number", getSocialSecurityNumber());

        }


}

佣金员工子类

public class CommissionEmployee extends Employee {

    //private final String firstName;
    //private final String lastName;
    //private final String socialSecurityNumber;
    private double grossSales; //gross weekly sales
    private double commissionRate; //commission percentage 

    //five argument constructor 
    public CommissionEmployee(String firstName, String lastName, String socialSecurityNumber, double grossSales, double commissionRate)
    {
        //implicit call to Object's default constructor occurs here 
        super(firstName,lastName,socialSecurityNumber);

        //if grossSales is invalid throw exception 
        if(grossSales < 0.0 )
            throw new IllegalArgumentException("Gross sales must be >=0.0");

        //if commissionRate is invalid throw Exception 
        if(commissionRate<= 0.0 && commissionRate >= 1.0)
            throw new IllegalArgumentException("Commission rate must be > 0.0 and < 1.0");

        //this.firstName = firstName;
        //this.lastName = lastName;
        //this.socialSecurityNumber = socialSecurityNumber;
        this.grossSales = grossSales;
        this.commissionRate = commissionRate;


    } //end constructor

    //return first name
    /*public String getFirstName()
    {
        return firstName;
    }

    //return last name
    public String getLastName()
    {
        return lastName;
    }

    //get Social Security number
    public String getSocialSecurityNumber(){
        return socialSecurityNumber;
    }
    */


    //set gross Sales amount
    public void setGrossSales(double grossSales)
    {
        if(grossSales <0.0)
            throw new IllegalArgumentException("Gross sales must be >= 0.0");
        this.grossSales = grossSales;
    }

    //return gross Sales amount 
    public double getGrossSales()
    {
        return grossSales;
    }

    //set commissionRate
    public void setCommissionRate(double commissionRate)
    {
        if(commissionRate <=0.0 || commissionRate >=1.0)
            throw new IllegalArgumentException("Commission rate must be >0.0 and <1.0");
        this.commissionRate = commissionRate;
    }

    //return commissionRate
    public double getCommissionRate()
    {
        return commissionRate;
    }

    //calculate Earnings 
    public double earnings()
    {
        return getCommissionRate() * getGrossSales();
    }


    //return String representation of CommissionEmployee object
     //indicates that this method overrides a superclass method 
    public String toString()
    {
        return String.format("%s: %.2f%n%s: %.2f", "commission employee",super.toString(),"gross sales",getGrossSales(),"commission rate", getCommissionRate());
        //return String.format("%s: %s %s%n%s: %s%n%s: %.2f%n%s: %.2f", "commission employee",getFirstName(), getLastName(), "social security number", getSocialSecurityNumber(),"gross sales",getGrossSales(),"commission rate", getCommissionRate());
    }




}

佣金员工测试Java

    public class CommissionEmployeeTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CommissionEmployee employee = new CommissionEmployee("Sam","randomlastname","123-34-5678",50.20,20.40);

        //get commission employee data
        System.out.println("Employee information obtained by get methods:");
        System.out.printf("%n%s %s%n", "First name is",employee.getFirstName());
        System.out.printf("%s %s%n", "Last name is", employee.getLastName());
        System.out.printf("%s %s%n", "Social Security number is", employee.getSocialSecurityNumber());
        System.out.printf("%s %.2f%n", "Gross Sales is", employee.getGrossSales());
        System.out.printf("%s %.2f%n", "Commission Rate is", employee.getCommissionRate());


        employee.setGrossSales(5000);
        employee.setCommissionRate(0.1);

        System.out.printf("%n%s:%n%n%s%n","Updated employee information obtained by toString",employee.toString());

    }

}

编辑:修复了我的问题

你们对我如何修复我的代码有什么建议吗?

Employee.toString() 中的 %s: %s %s%n%s: %s%n%s 中,我计算了 %s 出现六次,但只有五个参数。我认为您想删除最后一个 %s。