缺少格式参数异常错误

6

我已成功编译我的库存程序:

// Inventory.java part 1
// this program is to calculate the value of the inventory of the Electronics Department's cameras

import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;

public class Inventory
{
   public static void main(String[] args)
   {
       // create Scanner to obtain input from the command window
       Scanner input = new Scanner (System.in);

       String name;
       int itemNumber; // first number to multiply
       int itemStock; // second number to multiply
       double itemPrice; //
       double totalValue; // product of number1 and number2



   while(true){       // infinite loop
              // make new Camera object

      System.out.print("Enter Department name: "); //prompt
      String itemDept = input.nextLine(); // read name from user

            if(itemDept.equals("stop"))  // exit the loop
           break;


 {
 System.out.print("Enter item name: "); // prompt
 name = input.nextLine(); // read first number from user
 input.nextLine();


     }

 System.out.print("Enter the item number: "); // prompt
 itemNumber = input.nextInt(); // read first number from user
 input.nextLine();
  while( itemNumber <= -1){
 System.out.print("Enter valid number:"); // prompt
 itemNumber = input.nextInt(); // read first number from user input.nextLine();
 } /* while statement with the condition that negative numbers are entered
 user is prompted to enter a positive number */

 System.out.print("Enter number of items on hand: "); // prompt
 itemStock = input.nextInt(); // read first number from user
 input.nextLine();
  while( itemStock <= -1){
 System.out.print("Enter positive number of items on hand:"); // prompt
 itemStock = input.nextInt(); // read first number from user
 input.nextLine();
 } /* while statement with the condition that negative numbers are entered
 user is prompted to enter a positive number */

 System.out.print("Enter item Price: "); // prompt
 itemPrice = input.nextDouble(); // read second number from user
 input.nextLine();
  while( itemPrice <= -1){
 System.out.print("Enter positive number for item price:"); // prompt
 itemPrice = input.nextDouble(); // read first number from user
 input.nextLine();
 } /* while statement with the condition that negative numbers are entered
 user is prompted to enter a positive number */


 Cam camera = new Cam(name, itemNumber, itemStock, itemPrice);

 totalValue = itemStock * itemPrice; // multiply numbers

 System.out.println("Department name:" + itemDept); // display Department name
 System.out.println("Item number: " + camera.getItemNumber()); //display Item number
 System.out.println("Product name:" + camera.getName()); // display the item
 System.out.println("Quantity: " + camera.getItemStock());
 System.out.println("Price per unit" + camera.getItemPrice());
 System.out.printf("Total value is: $%.2f\n" + totalValue); // display product

       } // end while method

   } // end method main

}/* end class Inventory */

class Cam{

private String name;
private int itemNumber;
private int itemStock;
private double itemPrice;
private String deptName;

public Cam(String name, int itemNumber, int itemStock, double itemPrice) {
this.name = name;
this.itemNumber = itemNumber;
this.itemStock = itemStock;
this.itemPrice = itemPrice;
   }

   public String getName(){
      return name;
      }



   public int getItemNumber(){
   return itemNumber;

}

   public int getItemStock(){
   return itemStock;

}

   public double getItemPrice(){
    return itemPrice;

}

}

C:\Java>java Inventory
输入部门名称:电子产品
输入商品名称:相机
输入商品编号:12345
输入库存数量:8
输入商品价格:100.50
部门名称:电子产品
商品编号:12345
商品名称:相机
数量:8
单价:100.5
总价值为:
$Exception in thread "main" java.util.MissingFormatArgumentException:
格式说明符'.2f'
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
at java.io.PrintStream.printf(Unknown Source)
at Inventory.main(Inventory.java:82)

我似乎遇到了这个格式错误,但不知道原因。

3个回答

22

如果您正在使用printf,您需要将占位符与格式字符串一起指定为printf参数。在您的情况下,您通过附加金额来传递单个字符串,因此会出现错误。

System.out.printf("Total value is: $%.2f\n" + totalValue)

应该被替换为

System.out.printf("Total value is: $%.2f\n", totalValue)

2
同时在使用像%s这样的类型说明符与String.format()时也会发生这种情况。 - javaProgrammer

6

这是问题:

System.out.printf("Total value is: $%.2f\n" + totalValue);

我认为你的意思是:

System.out.printf("Total value is: $%.2f\n", totalValue);

换句话说,要指定替换占位符的值,而不仅仅是使用字符串拼接将该值添加到格式化字符串的末尾。
一般来说,当您遇到一个您不理解的异常时,应查看其文档。在这种情况下,文档相当清晰:
抛出未经检查的异常,当存在格式说明符没有相应的参数或者参数索引引用不存在的参数时。
因此,在您的代码中需要检查两件事:
- 是否有一个没有相应参数的格式说明符? - 是否有一个参数索引引用了不存在的参数?
由于您的格式化字符串中没有指定任何参数索引,所以必须是第一种情况 - 而事实上确实如此。

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接