简体   繁体   中英

I am having formatting issues

I want my program to format nicely like 这个

But, it formats like: 这个,间距不大

I have two classes, one of them is the getter one (Item), and the majority of the code will be in this one (TestItem)

This is the toString method in the getter file

    public String toString()
    {
        return " " + itemID + "\t" + itemName + "\t" + inStore + "\t" +  "$"  +     df.format(Price);//String.format("$%,1.2f", Price) +"\n" ;  
    }

And in TestMovie, Thses are the important methods:

   public static void main (String [] args)
   {
       Item[] hardware = new Item[6];

      hardware[0]=new Item(1011,"Air Filters",200,10.5);
      hardware[1]=new Item(1034,"Door Knobs",60,21.5);
      hardware[2]=new Item(1101,"Hammers",90,9.99);
      hardware[3]=new Item(1600,"Levels",80,19.99);
       hardware[4]=new Item(1500,"Ceiling Fans",100,59);
       hardware[5]=new Item(1201,"Wrench Sets",55,80);

       System.out.println("Original Array:");
      System.out.println();
      printInventory(hardware);
      System.out.println();

and this method traverses through the array

     public static void printInventory(Item[] hardware) //traverse and print
   {
       System.out.printf("itemID\titemName\tinStore\tprice\n"); 

  System.out.println("--------------------------------------------------------"); 
        for(Item print : hardware)
       {
            for (int i = 0; i < 1; i++)
            {
               //System.out.println(hardware[i]);
              System.out.printf(print.getItemID() + "\t" + print.getItemName() + "\t" + print.getInStore() + "\t$" + print.getPrice()); //format 
           }
           System.out.println();
       }
      }

The program runs without errors, It's just not formatted the way i would like it to be. I need implementing the printf function in my program. I'm more worried about the items, than the header.

As already proposed use printf() with format pattern instead of TAB characters. Then your printInventory() could look like

public static void printInventory(Item[] hardware) {
    System.out.printf("%-10s  %-20s  %-10s  %-10s%n",
            "itemID",
            "itemName",
            "inStore",
            "price"
    );

    System.out.println("--------------------------------------------------------");

    for (Item print : hardware) {
        System.out.printf("%-10s  %-20s  %-10s  $%,.2f%n",
                print.getItemID(),
                print.getItemName(),
                print.getInStore(),
                print.getPrice()
        );
    }
}

Some fine tuning is needed. But you should already get the big picture.

In your first line

System.out.println("itemID       itemName        inStore      price      "); 

You have also to use printf with tabulations and no space to get same indentation.

Adding tabs to your header:

System.out.printf("itemID\titemName\tinStore\tprice\t");

Will help get the first column in order. Your second problem is the length of the text in the title column which pushes your formatting out. A quick fix would be to add more tabs. But this may not work with short or longer titles.

As mentioned it is best to use the Formatter class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM