2016-04-08 194 views
0

當試圖從我的Account類打印信息時,它出現錯誤。爲什麼在使用printf時會出現運行時錯誤?

這裏是我的代碼:

System.out.printf("%5d $,9.2f %,5,2f%% %29s\n\n", account1.getId(), account1.getBalance(), account1.getAnnualInterestRate(), account1.getDateCreated()); 

和錯誤:

Exception in thread "main" java.util.UnknownFormatConversionException: Conversion = ',' 
    at java.util.Formatter.checkText(Unknown Source) 
    at java.util.Formatter.parse(Unknown Source) 
    at java.util.Formatter.format(Unknown Source) 
    at java.io.PrintStream.format(Unknown Source) 
    at java.io.PrintStream.printf(Unknown Source) 
    at TestAccount.printAccount(TestAccount.java:16) 
    at TestAccount.main(TestAccount.java:11) 

感謝

+0

它似乎在抱怨逗號。 – forgivenson

+0

也許試試「%5d $,9.2f%,5.2f %%%29s \ n \ n」? – totoro

+0

你想用'%,5,2f'實現什麼? – Pshemo

回答

3

試試這個:

System.out.printf("%5d$, %9.2f, %5.2f, %29s\n\n", ...); 

將打印的內容:

  • 一個整數填充到至少5個空格爲第一個參數
  • 浮子填充基數之前至少9米的空間,並且具有2位後的第二個參數
  • 浮子基數之前填充至少5個空格和有2位數字之後,用於第三個參數
  • 的字符串填充到至少29個字符爲所述參數
0

你有一個無效的轉換字符串:

%,5,2f 

不是有效轉換說明符。 (?你的意思是%5.2f

+0

就是這樣。這是爲了一個大學的任務,我給了格式字符串。我想我在複製/粘貼時沒有看到。謝謝 –

0

您的格式化%,5,2f%%,我認爲應該是%,5.2f%%

此外,您可能需要使用$%,9.2f代替$,9.2f

0

你的格式字符串畸形爲的IntelliJ示出:

enter image description here

0

看起來你已經在9.2f之後提出了「%」(比如9.2f%),我認爲你的意圖是獲得賬戶餘額,所以把9.2f之前的「%」(比如%9.2f )。以下是對我有用的糾正論據。 System.out.printf(「%5d $,%9.2f,5,%2f %%%29s」,account1.getId(),account1.getBalance(),account1.getAnnualInterestRate(),account1.getDateCreated ());

相關問題