0

我需要尽可能地格式化收据,就像普通收据一样。名称和地址,时间和日期都在顶部。(所有这些都需要用户输入。)

主要代码

    //Removed Imports

class ReceiptCode {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Font f = new Font("Calibri", Font.BOLD, 20);

        Scanner scan= new Scanner(System.in);
        System.out.println("Enter Company Name");
        String companyName= scan.nextLine();

        System.out.println("Enter STREET ADDRESS");
        String street=scan.nextLine();

        System.out.println("Enter CITY, STATE, ZIP");
        String CSZ=scan.nextLine();


        String breaker = "------------------------------";
        List <Items> invList = new ArrayList<Items>();
        System.out.println("How many items did you order?");
        int counter = scan.nextInt();
        double totalPrice = 0;
        for (int i=0; i<counter; i++)
        {
            System.out.println("Name the item");
            String fName = scan.next();
            System.out.println("How many of this item did you order?");
            int fType = scan.nextInt();
            System.out.println("What was the price?");
            double fPrice = scan.nextDouble();
            Items inv = new Items(fName, fType, fPrice);
            double x = (fType * fPrice);
            totalPrice += x;
            invList.add(inv);
            System.out.println(totalPrice);
        }

        DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        DateFormat timeFormat = new SimpleDateFormat ("HH:mm");
        Date date = new Date();
        Date time = new Date();
        System.out.printf("%-15s %n", companyName);
        System.out.printf("%-15s %14s %n",street + "\n" + CSZ,dateFormat.format(date));
        System.out.printf("%-15s %14s %n", timeFormat.format(time));
        System.out.println(breaker);
        for (Items c : invList) {
               System.out.println (c.getFoodAmmount() + " x " + c.getFoodName() + " : " + c.getFoodPrice() + "$");
               System.out.println (breaker);

}   
}
}       

这是我的 Items Array List 的类

数组列表

package Receipt;

public class Items {

        private String foodName;
        private int foodAmmount;
        private double foodPrice;

    public Items (String fdType, int fdAmmount, double fdPrice)
    {
        foodName = fdType;
        foodAmmount = fdAmmount;
        foodPrice = fdPrice;
    }
    public String getFoodName()
    {
        return foodName;
    }
    public int getFoodAmmount()
    {
        return foodAmmount;
    }
    public double getFoodPrice()
    {
        return foodPrice;
    }
}

当我编译代码时,我收到关于以下内容的异常:Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '14s'. 我将如何解决这个问题?

4

1 回答 1

0

您收到异常是因为在时间线上您没有设置 %14s(它在 printf 语句中找不到变量)。

System.out.printf("%-15s %14s %n", timeFormat.format(time));

此行包含错误。如果您将其更改为:

System.out.printf("%-15s %n", timeFormat.format(time));

它应该可以正常工作。

于 2013-11-06T21:07:11.070 回答