-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();
    }

i have this question with using printf in java, so this is the error i get with 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)

2 Answers2

0

This

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

will not work because the last is a number and you pass it the string "PRICE".

You should use different format string for the columns names.

Column names : "\n%-5s%-5s%-15s%-15s%-6s%-15s%-5s\n"

Zpetkov
  • 175
  • 1
  • 8
0

Change the line

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

to

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

because the last part %-5d\n" will try to get int value but you want to print a string i.e. "PRICE"

Rest is fine.

Simple mistake

Doc
  • 10,831
  • 3
  • 39
  • 63