2015-11-07 88 views
-3
void catalog() 
    { 
     System.out.printf("\n%-5s%-5s%-15s%-15s%-6s%-15s%-5d\n", 
      "Sno.","B.No.","BOOK-NAME","AUTHOR-NAME","COPIES","PUBLISHER","PRICE"); 
     for(int i=1;i<53;i++) 
      System.out.print("-"); 
     System.out.println(); 
     for(int i=0;i<nob;i++) 
      System.out.printf("%-5d%-5d%-15s%-15s%-5d%-10s%-10d\n", 
       (i+1),bno[i],bname[i],author[i],availcopies[i],publisher[i],price[i]); 
     for(int i=1;i<53;i++) 
      System.out.print("-"); 
     System.out.println(); 
    } 

我有在Java中使用的printf這個問題,所以這是錯誤我用的printf得到的Java printf的幫助中格式化

java.util.MissingFormatArgumentException: Format specifier '%-15s' 
    at java.util.Formatter.format(Formatter.java:2519) 
    at java.io.PrintStream.format(PrintStream.java:970) 
    at java.io.PrintStream.printf(PrintStream.java:871) 
    at Library.catalog(LibrarySystem.java:162) 
    at LibrarySystem.main(LibrarySystem.java:218) 
+0

你想要做什麼? –

+0

我想打印一張表,列出數組的細節 –

+0

嘗試使用' - %s'而不是'%-s' –

回答

0

更改線路

System.out.printf("\n%-5s%-5s%-15s%-15s%-6s%-15s%-5d\n", 
      "Sno.","B.No.","BOOK-NAME","AUTHOR-NAME","COPIES","PUBLISHER","PRICE"); 

System.out.printf("\n%-5s%-5s%-15s%-15s%-6s%-15s%-5s\n", 
      "Sno.","B.No.","BOOK-NAME","AUTHOR-NAME","COPIES","PUBLISHER","PRICE"); 

因爲最後一部分%-5D \ n」將努力讓int值,但你想要打印字符串,即「PRICE」

休息很好。

簡單的錯誤

0

System.out.printf("\n%-5s%-5s%-15s%-15s%-6s%-15s%-5d\n", 
    "Sno.","B.No.","BOOK-NAME","AUTHOR-NAME","COPIES","PUBLISHER","PRICE"); 

不會因爲工作最後是一個數字,你把它傳遞給字符串「PRICE」。

您應該爲列名稱使用不同的格式字符串。

列名:"\n%-5s%-5s%-15s%-15s%-6s%-15s%-5s\n"