简体   繁体   中英

PrintWriter new line issue. Working fine in Eclipse

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?

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());
}

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 (eg UTF-8 or Windows-1252) as javac expects.

You can replace "\\n" with "\\r\\n" and see if it helps. I suspect if this is the interpretation of the line separator by the app. For example, there is '\\n' (the LF character) in the code, but the app you use to read the file does not interpret it into a new line.

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