1

I have to get my output to line up beneath a heading. No matter what I do, I cannot get to line up. The item name is very long also, and the words end up wrapping to the next line when I open my outfile. Here is my current output:

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

Here is my code:

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

Thanks for any help! I have tried many things for hours!!

4

1 回答 1

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  
于 2013-10-21T01:06:01.393 回答