2011-10-02 6 views
6

Sono riuscito a compilare il mio programma di inventario:MissingFormatArgumentException errore

// 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 inventario
Inserire il nome Dipartimento: Elettronica
Immettere il nome del prodotto: fotocamera
Inserisci la voce numero: 12345
Inserisci il numero di elementi a disposizione: 8
Inserisci articolo Prezzo: 100.50
Nome del Dipartimento e: Elettronica
numero dell'oggetto: 12345
Nome prodotto: Fotocamera
Quantità: 8
Prezzo per unit100.5
valore totale è:
$ Exception in thread java.util.MissingFormatArgumentException "principale":
di formato '.2f'
a java.util.Formatter.format (fonte sconosciuta)
a java.io.PrintStream.format (Fonte sconosciuta)
a java.io.PrintStream.printf (Fonte sconosciuta)
a Inventory.main (Inventory.java:82)

Mi sembra di venire in questo errore di formato e non vedo perché.

risposta

19

Se si utilizza printf, è necessario specificare i segnaposto come parametri printf insieme alla stringa di formato. Nel tuo caso stai passando una singola stringa aggiungendo l'importo quindi l'errore.

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

dovrebbe essere sostituito da

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

Si verifica anche durante l'utilizzo di String.format() con identificatori di tipo come% s – javaProgrammer

6

Questo è il problema:

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

Penso che volevi dire:

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

In altre parole, specificare il valore per sostituire il segnaposto con, anziché utilizzare semplicemente la concatenazione di stringhe per aggiungere il valore all'invio della stringa di formato.

In generale, tuttavia, quando si ottiene un'eccezione che non si capisce, è necessario consultare la documentazione. In questo caso, il docs are reasonably clear:

eccezione incontrollato generata quando v'è un identificatore di formato che non ha un parametro corrispondente oppure se un indice argomentazione si riferisce ad un argomento che non esiste.

Quindi ci sono due cose che dovete controllare nel codice:

  • Hai un identificatore di formato senza un corrispondente argomento?
  • Hai un indice di argomenti che fa riferimento a un argomento che non esiste?

Non è stato specificato alcun indice di argomenti nella stringa del formato, quindi deve essere il primo caso, che effettivamente è.

Problemi correlati