Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PrintWriter new line issue. Working fine in Eclipse

Tags:

java

Basically I have the following method that is a bit overkill I know, but I'm learning.....

public String showOrder() {
        String x = "";
        x = "Order Summary: \n ";
        for (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 is returning in my test program as I'd expect with each order on its own line.

 Product Id: 1...etcetc
 Product Id  2...etcetc
 Product Id  3...etcetc

But, when I create a PrintWriter the showOrder() method prints out one long list in my .txt. file.

PrintWriter cartReceipt = new PrintWriter("Cart.txt");

        cartReceipt.println(cart1.showOrder());
        cartReceipt.flush();
        cartReceipt.close();

Is there a way to make the same method return a nice orderly list in the txt file like it does in the IDE?

like image 493
Matt Avatar asked Mar 14 '23 13:03

Matt


2 Answers

The issue is \n is not a portable line ending. You 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, it would be more efficient if you streamed the write operation to the PrintWriter (since you don't seem to use the String except for writing the orders). You could pass the PrintWriter into the writing method. Something like,

public void writeOrder(PrintWriter pw) {
    pw.println("Order Summary:");
    for (Order order : orders) {
        // NOTE: If possible, I would move this code into Order.toString()
        //       and then 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());
}
like image 119
Elliott Frisch Avatar answered Mar 23 '23 15:03

Elliott Frisch


You are using "\n" as a newline character. If you are using Notepad to read the .txt file, it doesn't recognize the '\n' character as a newline (it is normally "\r\n" on Windows) and therefore displays all lines on the same line. If you use your IDE to read the .txt file, it will probably display it the way you expect.

The correct way to write your code is to use System.lineSeparator(). Another change you ought to make in your code is purely for performance reasons: if you are building a string one piece at a time in a loop, you should use a StringBuilder instead of String:

public String showOrder() {
    StringBuilder x = new StringBuilder("Order Summary:");
    x.append(System.lineSeparator());
    for (Order order : orders) {
        x.append("    Product Id: ").append(order.getProductCode()
            .append("    Product Description: ").append(order.getProductDesc())
            .append("    Order Quantity: ").append(order.getQuantity())
            .append("    Order Cost: £").append(order.getCost())
            .append(System.lineSeparator());
    }
    x.append("Total Cost: ").append(getTotalCost())
        .append("p        Number of Orders: ").append(numOfOrders())
        .append("     Total Products: ").append(numOfProducts())
        .append(System.lineSeparator());
    return x.toString();
}

Lastly, be careful when you use non-ASCII characters like "£" in your source file. Make sure you are using the same character encoding (e.g. UTF-8 or Windows-1252) as javac expects.

like image 25
Klitos Kyriacou Avatar answered Mar 23 '23 15:03

Klitos Kyriacou