2011-10-02 15 views
6

I udało się kompilowania mój program magazynowy:MissingFormatArgumentException błąd

// 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> Inventory java
Wpisz nazwę Dział: Electronics
Wpisz nazwę produktu: aparat
Wprowadź przedmiot numer: 12345
Podaj liczbę przedmiotów na wyciągnięcie ręki: 8
Wpisz przedmiot Cena: 100,50
Nazwa działu e: Elektronika
liczba produktu: 12345
nazwa produktu: kamera
Ilość: 8
Cena za unit100.5
Łączna wartość jest:
$ Wyjątek w wątku "głównym" java.util.MissingFormatArgumentException:
Format specifier '.2f'
na java.util.Formatter.format (Unknown Source)
na java.io.PrintStream.format (Unknown Source)
na java.io.PrintStream.printf (Unknown Source)
w Inventory.main (Inventory.java:82)

Wydaje mi się, że przychodzę do tego błędu formatu i nie widzę powodu.

Odpowiedz

19

Jeśli używasz printf, musisz określić symbole zastępcze jako parametry printf wraz z łańcuchem formatu. W twoim przypadku podajesz pojedynczy ciąg przez dodanie kwoty, stąd błąd.

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

należy zastąpić

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

Występuje również podczas używania String.format() ze specyfikatorami typu jak% s – javaProgrammer

6

To jest problem:

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

Chyba masz na myśli:

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

Innymi słowy, należy podać wartość zastępującą symbol zastępczy, zamiast po prostu użyć łączenia ciągów, aby dodać wartość do wysyłania ciągu formatu.

Ogólnie, gdy pojawi się wyjątek, którego nie rozumiesz, powinieneś przejrzeć jego dokumentację. W tym przypadku, docs are reasonably clear:

Niesprawdzona wyjątkiem generowany, gdy jest specyfikator formacie, który nie posiada odpowiedniej argumentu lub indeks argument odnosi się do argumentów, które nie istnieją.

Więc są dwie rzeczy, które trzeba sprawdzić w kodzie:

  • Czy masz specyfikator formatu bez odpowiedniego argumentu?
  • Czy istnieje indeks argumentów, który odnosi się do argumentu, który nie istnieje?

Nie podano żadnych indeksów argumentów w ciągu znaków formatowania, więc musi to być pierwszy przypadek - który rzeczywiście jest.