java - I am having formatting issues -
i want program format nicely
but, formats like:
i have 2 classes, 1 of them getter 1 (item), , majority of code in 1 (testitem)
this tostring method in 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 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 method traverses through array
public static void printinventory(item[] hardware) //traverse , print { system.out.printf("itemid\titemname\tinstore\tprice\n"); system.out.println("--------------------------------------------------------"); for(item print : hardware) { (int = 0; < 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 not formatted way be. need implementing printf function in program. i'm more worried items, header.
as proposed use printf()
format pattern instead of tab characters. printinventory()
like
public static void printinventory(item[] hardware) { system.out.printf("%-10s %-20s %-10s %-10s%n", "itemid", "itemname", "instore", "price" ); system.out.println("--------------------------------------------------------"); (item print : hardware) { system.out.printf("%-10s %-20s %-10s $%,.2f%n", print.getitemid(), print.getitemname(), print.getinstore(), print.getprice() ); } }
some fine tuning needed. should big picture.
Comments
Post a Comment