MissingFormatArgumentException错误
我成功编制了库存计划:
// 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
总价值是:
线程“main”中的$ Exception java.util.MissingFormatArgumentException:
格式说明符’.2f’
at java.util.Formatter.format(Unknown Source)
在java.io.PrintStream.format(未知来源)
在java.io.PrintStream.printf(未知来源)
在Inventory.main(Inventory.java:82)
我似乎遇到这种格式错误,无法理解为什么。
如果使用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);
换句话说,指定用于替换占位符的值,而不是仅使用字符串连接将值添加到格式字符串的发送中。
一般来说,当你得到一个你不理解的例外时,你应该看看它的文档。 在这种情况下, 文档相当清楚 :
当存在没有相应参数的格式说明符或参数索引引用不存在的参数时,抛出未经检查的exception。
因此,您需要在代码中检查两件事:
- 你有没有相应参数的格式说明符吗?
- 你有一个参数索引,它引用了一个不存在的参数吗?
您没有在格式字符串中指定任何参数索引,因此它必须是第一种情况 – 实际上它是。
System.out.printf("Total value is: $%.2f\n", totalValue); // display product
– > http://www.java2s.com/Code/JavaAPI/java.lang/Systemoutprintf2ffloatf.htm
- java hmac / sha512代
- 使用Maven在IntelliJ中构建时解决PermGen错误?
- 寻找HttpServletResponseWrapper的捕获impl
- 在自定义控制器中接受Spring Data REST URI
- 为什么Java的语言设计者更倾向于对大多数基于散列的结构的开放寻址进行链接,除了像ThreadLocal这样的结构?
- 字符串连接真的那么慢吗?
- 范围报告report.endTest(测试)方法?
- 错误:java.io.IOException:错误的值类:class org.apache.hadoop.io.Text不是类Myclass
- 模型驱动的架构是否值得,工具的最新技术是什么?