java - PrintWriter new line issue. Working fine in Eclipse -
basically have following method bit overkill know, i'm learning.....
public string showorder() { string x = ""; x = "order summary: \n "; (order order : orders) { x += " product id: " + order.getproductcode() + " product description: " + order.getproductdesc() + " order quantity: " + order.getquantity() + " order cost: £" + order.getcost() + "\n "; } x += "total cost: "+ gettotalcost() + "p number of orders: " + numoforders() + " total products: " + numofproducts(); return x; }
this returning in test program i'd expect each order on own line.
product id: 1...etcetc product id 2...etcetc product id 3...etcetc
but, when create printwriter showorder() method prints out 1 long list in .txt. file.
printwriter cartreceipt = new printwriter("cart.txt"); cartreceipt.println(cart1.showorder()); cartreceipt.flush(); cartreceipt.close();
is there way make same method return nice orderly list in txt file in ide?
the issue \n
not portable line ending. can use system.lineseparator()
like
x += " product id: " + order.getproductcode() + " product description: " + order.getproductdesc() + " order quantity: " + order.getquantity() + " order cost: £" + order.getcost() + system.lineseparator();
however, more efficient if streamed write operation printwriter
(since don't seem use string
except writing orders). pass printwriter
writing method. like,
public void writeorder(printwriter pw) { pw.println("order summary:"); (order order : orders) { // note: if possible, move code order.tostring() // , use pw.println(order); pw.print(" product id: "); pw.print(order.getproductcode()); pw.print(" product description: "); pw.print(order.getproductdesc()); pw.print(" order quantity: "); pw.print(order.getquantity()); pw.print(" order cost: £"); pw.println(order.getcost()); } pw.print("total cost: "); pw.print(gettotalcost()); pw.print("p number of orders: "); pw.print(numoforders()); pw.print(" total products: "); pw.println(numofproducts()); }
Comments
Post a Comment