Question

I need to make a receipt formatted as best i can like a normal receipt. With a name and address, time and date all at the top. (all of which need to be user input.)

main code

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

}   
}
}       

This is the class for my Items Array List

ArrayList

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;
    }
}

When I compile the code I recieve an exception regarding: Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '14s'. How would I go about fixing this problem?

Was it helpful?

Solution

You are receiving the exception because on the Time line you are not setting the %14s (it cannot find the variable in the printf statement).

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

This line contains the error. If you change it to:

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

It should work correctly.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top