我无法将此代码排在标题下方

时间:2013-10-21 00:52:28

标签: java formatting hardware store printwriter

我必须让我的输出排在标题下方。无论我做什么,我都无法排队。项目名称也很长,当我打开outfile时,单词最终会换行到下一行。这是我目前的输出:

8  items are currently available for purchase in Joan's Hardware Store.
----------Joan's Hardware Store-----------
itemID itemName pOrdered pInStore pSold manufPrice sellPrice
 1111  Dish Washer                      20 20 0 250.50 550.50

 2222  Micro Wave                       75 75 0 150.00 400.00

 3333  Cooking Range                    50 50 0 450.00 850.00

 4444  Circular Saw                     150 150 0 45.00 125.00

 5555  Cordless Screwdriver Kit         10 10 0 250.00 299.00

 6666  Keurig Programmable Single-Serve 2 2 0 150.00 179.00

 7777  Moen Chrome Kitchen Faucet       1 1 0 90.00 104.00

 8888  Electric Pressure Washer         0 0 0 150.00 189.00
Total number of items in store: 308
Total inventory: $: 48400.0

这是我的代码:

public void endOfDay(PrintWriter outFile)
    {
    outFile.println (nItems + "  items are currently available for purchase in Joan's Hardware Store.");
    outFile.println("----------Joan's Hardware Store-----------");

    outFile.printf("itemID, itemName, pOrdered, pInStore, pSold, manufPrice, sellPrice");   
        for (int index = 0; index < nItems; index++)
        {

outFile.printf("%n %-5s %-32s %d %d %d %.2f %.2f%n", items[index].itemID  , items[index].itemName  , items[index].numPord  ,items[index].numCurrInSt  , items[index].numPSold  , items[index].manuprice  , items[index].sellingprice);
        }
        outFile.println("Total number of items in store: " + getTotalOfStock());
        outFile.println("Total inventory: $: " + getTotalDollarValueInStore());
    }  // end endOfDay

感谢您的帮助!我已经尝试了好几个小时了!!

1 个答案:

答案 0 :(得分:1)

基本上,您需要按照格式化线条的方式格式化标题,例如......

System.out.println("----------Joan's Hardware Store-----------");
System.out.printf("%-6s %-32s %-8s %-8s %-5s %-10s %-8s%n", "itemID", "itemName", "pOrdered", "pInStore", "pSold", "manufPrice", "sellPrice");
System.out.printf("%-6s %-32s %-8d %-8d %-5d %-10.2f %-8.2f%n", "1111", "Dish Washer", 20, 20, 0, 250.50, 550.50);

结果......

----------Joan's Hardware Store-----------
itemID itemName                         pOrdered pInStore pSold manufPrice sellPrice
1111   Dish Washer                      20       20       0     250.50     550.50