MissingFormatArgumentException 错误

2022-01-15 00:00:00 format java inventory

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

//Inventory.java 第 1 部分//这个程序是计算电子部相机库存的价值导入 java.util.*;导入 javax.swing.*;导入 java.awt.event.*;导入java.io.*;公共类库存{公共静态无效主要(字符串 [] 参数){//创建扫描器以从命令窗口获取输入扫描仪输入 = 新扫描仪(System.in);字符串名称;int 项目编号;//第一个要相乘的数字int itemStock;//第二个要相乘的数字双项价格;//双倍总价值;//number1 和 number2 的乘积while(true){//无限循环//创建新的相机对象System.out.print("请输入部门名称:");//迅速的String itemDept = input.nextLine();//读取用户名if(itemDept.equals("stop"))//退出循环休息;{System.out.print("请输入项目名称:");//迅速的名称 = input.nextLine();//读取用户的第一个数字input.nextLine();}System.out.print("请输入商品编号:");//迅速的itemNumber = input.nextInt();//读取用户的第一个数字input.nextLine();而(项目编号 <= -1){System.out.print("请输入有效数字:");//迅速的itemNumber = input.nextInt();//从用户 input.nextLine() 中读取第一个数字;}/* while 语句,条件是输入负数提示用户输入正数 */System.out.print("输入手头的物品数量:");//迅速的itemStock = input.nextInt();//读取用户的第一个数字input.nextLine();而(项目库存 <= -1){System.out.print("输入正数的手头物品数:");//迅速的itemStock = input.nextInt();//读取用户的第一个数字input.nextLine();}/* while 语句,条件是输入负数提示用户输入正数 */System.out.print("输入商品价格:");//迅速的itemPrice = input.nextDouble();//从用户那里读取第二个数字input.nextLine();而(项目价格 <= -1){System.out.print("请输入商品价格的正数:");//迅速的itemPrice = input.nextDouble();//读取用户的第一个数字input.nextLine();}/* while 语句,条件是输入负数提示用户输入正数 */Cam camera = new Cam(name, itemNumber, itemStock, itemPrice);totalValue = itemStock * itemPrice;//乘数System.out.println("部门名称:" + itemDept);//显示部门名称System.out.println("物品编号:" + camera.getItemNumber());//显示商品编号System.out.println("产品名称:" + camera.getName());//显示项目System.out.println("数量:" + camera.getItemStock());System.out.println("单价" + camera.getItemPrice());System.out.printf("总值为:$%.2f
" + totalValue);//显示产品}//结束 while 方法}//结束方法 main}/* 结束类库存 */类凸轮{私有字符串名称;私人 int itemNumber;私有 int itemStock;私人双项价格;私人字符串部门名称;公共凸轮(字符串名称,int itemNumber,int itemStock,double itemPrice){this.name = 名称;this.itemNumber = itemNumber;this.itemStock = itemStock;this.itemPrice = itemPrice;}公共字符串 getName(){返回名称;}公共 int getItemNumber(){返回项目编号;}公共 int getItemStock(){返回项目库存;}公共双 getItemPrice(){返回项目价格;}}

<块引用>

C:Java>java 库存
输入部门名称:电子
输入项目名称:相机
输入货号:12345
输入手头的物品数量:8
输入商品价格:100.50
部门名称:电子学
货号:12345
产品名称:相机
数量:8
单价100.5
总值是:
线程主"java.util.MissingFormatArgumentException 中的 $Exception:
格式说明符.2f"
在 java.util.Formatter.format(未知来源)
在 java.io.PrintStream.format(未知来源)
在 java.io.PrintStream.printf(未知来源)
在 Inventory.main(Inventory.java:82)

我似乎遇到了这种格式错误,不明白为什么.

解决方案

如果你使用printf,你需要将占位符指定为<​​code>printf参数以及格式细绳.在您的情况下,您通过附加金额来传递单个字符串,因此会出现错误.

System.out.printf("总值为:$%.2f
" + totalValue)

应该替换为

System.out.printf("总值为:$%.2f
", totalValue)

I have been successful in compiling my inventory program:

// 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
" + 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
Enter Department name: Electronics
Enter item name: camera
Enter the item number: 12345
Enter number of items on hand: 8
Enter item Price: 100.50
Department name:Electronics
Item number: 12345
Product name:camera
Quantity: 8
Price per unit100.5
Total value is:
$Exception in thread "main" java.util.MissingFormatArgumentException:
Format specifier '.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)

I seem to come to this format error and cannot see why.

解决方案

If you are using printf, you need to specify the placeholders as printf parameters along with the format string. In your case you are passing a single string by appending the amount hence the error.

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

should be replaced by

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

相关文章