2012-06-05 18 views
5

ho avuto un codice perfettamente funzionante ieri, nella forma precisa di:"IllegalFormatConversionException: d! = Java.lang.String" quando il numero di riempimento con 0s?

int lastRecord = 1; 
String key = String.format("%08d", Integer.toString(lastRecord)); 

che avrebbe pad è ben al 00000001.

Ora ho preso a calci su una tacca con twoKeyChar ottenere una stringa da una tabella e lastRecord ottenere un int da una tabella.

Come potete vedere il concetto è essenzialmente lo stesso - converto un int in una stringa e provo a riempirlo con 0s; tuttavia, questa volta ottengo il seguente errore:

java.util.IllegalFormatConversionException: d != java.lang.String 

Il codice è qui sotto:

String newPK = null; 
String twoCharKey = getTwoCharKey(tablename); 
if (twoCharKey != null) { 
    int lastRecord = getLastRecord(tablename); 
    lastRecord++; 
    //The println below outputs the correct values: "RU" and 11. 
    System.out.println("twocharkey:"+twoCharKey+"record:"+lastRecord+"<"); 
    //Now just to make it RU00000011 
    newPK = String.format("%08d", Integer.toString(lastRecord)); 
    newPK = twoCharKey.concat(newPK); 
} 

Mi sento come se devo aver digitato qualcosa di sbagliato, perché non v'è alcun motivo per la rottura in quanto la l'ultima volta in cui ha funzionato. Qualsiasi aiuto/suggerimento è apprezzato! Grazie!

+1

In forma umana, che dice "l'identificatore di formato' d' non si applica ai valori di stringa ". –

risposta

12

non ti servono il Integer.toString():

newPK = String.format("%08d", lastRecord); 

String.format() farà la conversione e l'imbottitura.

+0

Ho intenzione di andare al mio angolo, sentirsi stupido ... Bella cattura! Grazie! Accetterà risposta in 10 minuti quando mi consente, heh. –

+2

Non è che non ti sia NECESSARIO fare il toString, ma più non puoi. La "d" nel primo argomento in formato String prevede un numero intero decimale. Quindi l'errore. –

Problemi correlati