0

it's my first time to do java. We are currently learning it but we have not really touched up on formatting and i was wondering if anyone could help me with my code. So my code is as follows:

public void displayAllOrder()
{
    System.out.println("Initial Cost" + "\t" + "Voucher Code Used" + "\t" + "Voucher Amount Deducted" + "\t\t" + "Final Cost" + "\t" + "Shipping ID" + "\t" + "Shipment Status");
    for (int i = 0; i <= currentIndex; i++)
    {
        String strInitialCost = String.format("%.2f", initialCost[i]);
        String strFinalCost = String.format("%.2f", finalCost[i]);
        System.out.println("$"+strInitialCost + "\t\t" + voucherCode[i] + "\t\t\t" + "$"+voucherAmount[i] + "\t\t\t" + "$"+strFinalCost + "\t" + shippingID[i] + "\t\t" + shipmentStatus[i]);
    }

It aligns perfectly if the input is large but it is not aligned if the calculated input is small. Example output as follows: enter image description here

I hope someone could help me understand with regards to this issue. Thank you.

5
  • Tabs are finicky and it is better to use spaces for alignment. You also need to be sure your font is monospaced. The String.format method supports field sizes that can help with this. The code %10s will print one argument padded to at least size 10. It won't truncate, though. Commented Sep 17, 2021 at 13:52
  • I think the problem is not java, if you try to write the same thing in a notepad for example, you are going to have the same problem. Therefore if you want always everything to be aligned, you have to calculate the tabs depending on the input. Commented Sep 17, 2021 at 13:52
  • 1
    Don't use tabs to align output. See this answer Commented Sep 17, 2021 at 13:53
  • You also need to be aware of the difference of left alignment or right alignment, which just means you put the extra spaces on the left or right to pad a field to a column width. Commented Sep 17, 2021 at 13:53
  • change the voucher code from 0 to 0000, that would align rest of format Commented Sep 17, 2021 at 13:53

2 Answers 2

2

Maybe you even want a different output type.

When it's about layout maybe you want to put it in an html table and watch it with a browser:

public void displayAllOrder()
{
    System.out.println("<html><table><thead><tr>");
    for(String title : Arrays.asList("Initial Cost", "Voucher Code Used", "Voucher Amount Deducted", "Final Cost", "Shipping ID", "Shipment Status"))
    {
        printCell(title);
    }
    System.out.println("</tr></thead><tbody>");
    for (int i = 0; i <= currentIndex; i++)
    {
        System.out.println("<tr>");
        String strInitialCost = String.format("%.2f", initialCost[i]);
        String strFinalCost = String.format("%.2f", finalCost[i]);
        printCell("$"+strInitialCost);
        printCell(voucherCode[i]);
        printCell("$"+voucherAmount[i]);
        printCell("$"+strFinalCost);
        printCell(shippingID[i]);
        printCell(shipmentStatus[i]);
        System.out.println("</tr>");
    }
    System.out.println("</tbody></table></html>");
}

private void printCell(String content)
{
    System.out.println("<td>" + content + "</td>");
}
Sign up to request clarification or add additional context in comments.

Comments

1

For a fixed sized font where every character has the same width ("m" and "i"), you can use printf with something like %13.2f for a 13 positions right aligned double.

public void displayAllOrders() {
    System.out.printf("%-15s %-15s %-20s %-15s %-15s %s%n",
        "Initial Cost", "Voucher Code Used", "Voucher Amount Deducted",
        "Final Cost", "Shipping ID", "Shipment Status");
    for (int i = 0; i <= currentIndex; i++) {
        System.out.printf("$-14.2f %-15s %5s$%14.2f $%14.2f %-15s %s%n",
          initialCost[i], voucherCode[i], "", voucherAmount[i],
          finalCost[i], shippingID[i], shipmentStatus[i]);
    }
}

The above code might be wrong, and I do not know the widths needed: columns 15 wide, one space between them. %n = end of line.

Using tabs, "\t" will on Windows pad to 8 spaces. That will lead to unnerving layouting.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.