Java MissingFormatArgumentException错误

Java MissingFormatArgumentException错误,java,format,inventory,Java,Format,Inventory,我成功地编制了我的库存计划: // 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 {

我成功地编制了我的库存计划:

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

}

}
//Inventory.java第1部分
//此程序用于计算电子部门摄像机的库存值
导入java.util.*;
导入javax.swing.*;
导入java.awt.event.*;
导入java.io.*;
公共类目录
{
公共静态void main(字符串[]args)
{
//创建扫描仪以从命令窗口获取输入
扫描仪输入=新扫描仪(System.in);
字符串名;
int itemNumber;//要乘以的第一个数字
int itemStock;//要乘以的第二个数字
双倍价格//
double totalValue;//number1和number2的乘积
while(true){//无限循环
//创建新的摄影机对象
System.out.print(“输入部门名称:”;//提示
String itemDept=input.nextLine();//从用户处读取名称
如果(itemDept.equals(“stop”)//退出循环
打破
{
System.out.print(“输入项目名称:”;//提示
name=input.nextLine();//从用户处读取第一个数字
input.nextLine();
}
System.out.print(“输入项目编号:”;//提示
itemNumber=input.nextInt();//从用户处读取第一个数字
input.nextLine();

而(itemNumber如果使用的是
printf
,则需要将占位符指定为
printf
参数以及格式字符串。在您的情况下,通过追加金额传递单个字符串,从而产生错误

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

System.out.printf("Total value is: $%.2f\n", totalValue)
->这就是问题所在:

System.out.printf("Total value is: $%.2f\n" + totalValue);
我想你的意思是:

System.out.printf("Total value is: $%.2f\n", totalValue);
换句话说,指定要替换占位符的值,而不是仅使用字符串连接将值添加到格式字符串的发送中

但是,一般来说,当您遇到不理解的异常时,您应该查看文档

当格式说明符没有相应的参数或参数索引引用不存在的参数时,引发未经检查的异常

因此,您需要在代码中签入两件事:

  • 是否有没有没有相应参数的格式说明符
  • 您是否有引用不存在的参数的参数索引

您尚未在格式字符串中指定任何参数索引,因此它必须是第一种情况-事实上是这样。

在使用string.format()和类型说明符(如%s)时也会发生这种情况
System.out.printf("Total value is: $%.2f\n", totalValue);