简体   繁体   中英

Exception in thread “main” java.util.UnknownFormatConversionException: Conversion = '.'?

No matter what I input with this formatting it does this error message. I just need to get two decimal places so it'll represent a dollar amount. Any help?

Scanner keyboard=new Scanner(System.in);

  System.out.println("Enter theater number: ");
  theater=keyboard.nextInt();

  while(theater!=-999)
  {
     while(theater==1)
     {
        System.out.println("Enter number of adult tickets: ");
        adultTickets=keyboard.nextInt();

        System.out.println("Enter number of child tickets: ");
        childTickets=keyboard.nextInt();

        finalTicketsAdult1+=adultTickets;
        finalTicketsChild1+=childTickets;

        System.out.println("Enter theater number: ");
        theater=keyboard.nextInt();
     }

     while(theater==2)
     {
        System.out.println("Enter number of adult tickets: ");
        adultTickets=keyboard.nextInt();

        System.out.println("Enter number of child tickets: ");
        childTickets=keyboard.nextInt();

        finalTicketsAdult2+=adultTickets;
        finalTicketsChild2+=childTickets;

        System.out.println("Enter theater number: ");
        theater=keyboard.nextInt();
     }

  System.out.println("Theater 1\n");
  System.out.println("Number of Adult Tickets: " + finalTicketsAdult1);
  System.out.println("Number of Child Tickets: " + finalTicketsChild1);
  totalSalesThtr1=(finalTicketsAdult1*9.50 + finalTicketsChild1*6.00);
  System.out.printf("Total sales: $%.2",totalSalesThtr1 + "\n");

  System.out.println("Theater 2\n");
  System.out.println("Number of Adult Tickets: " + finalTicketsAdult2);
  System.out.println("Number of Child Tickets: " + finalTicketsChild2);
  totalSalesThtr2=(finalTicketsAdult2*12.50 + finalTicketsChild2*7.50);
  System.out.printf("Total sales: $%.2",totalSalesThtr2 + "\n");

  overallSales=totalSalesThtr1 + totalSalesThtr2;

  System.out.printf("Overall sales: $%.2",overallSales);
  }}}


   Enter theater number: 
    1
   Enter number of adult tickets: 
    5
   Enter number of child tickets: 
   5
   Enter theater number: 
    2
   Enter number of adult tickets: 
   5
  Enter number of child tickets: 
   5
  Enter theater number: 
  -999
 Theater 1

 Number of Adult Tickets: 5
 Number of Child Tickets: 5
 Exception in thread "main" java.util.UnknownFormatConversionException:     Conversion = '.'
at java.util.Formatter.checkText(Formatter.java:2547)
at java.util.Formatter.parse(Formatter.java:2533)
at java.util.Formatter.format(Formatter.java:2469)
at java.io.PrintStream.format(PrintStream.java:970)
at java.io.PrintStream.printf(PrintStream.java:871)

I have no idea how to fix this as I would like to think I'm doing this right. Is there a reason or way to fix this?

This

System.out.printf("Total sales: $%.2",totalSalesThtr1 + "\n");

should be

System.out.printf("Total sales: $%.2f%n", totalSalesThtr1);

and the same with the other printf calls

System.out.printf("Total sales: $%.2f%n", totalSalesThtr2);
// ...
System.out.printf("Overall sales: $%.2f%n", overallSales);

The f converter indicates that the value is floating point and %n adds a newline.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM