Ingram
Ingram

Reputation: 674

lining up data in console output java

I have the following extract of code below;

The issue is that

(element.getChildNodes().item(0).getNodeValue())

has output of a differing number of characters

which then causes eventy to be moved out of line with other output from the other rows therefore giving a zig zag appearance of data instead of data in columns, i have tried experimenting with tabs and spaces. But would appreciate some help please.

String eventy = null;

for (int i = 0; i < list.getLength(); i++) {
    Element element = (Element)list.item(i);
    String nodeName = element.getNodeName();

    switch (nodeName) {
        case "assd":
            System.out.println((element.getChildNodes().item(0).getNodeValue()) + "\t  " eventy);
            break;
        case "eventy":
            eventy = element.getChildNodes().item(0).getNodeValue();
            break;
    }
}

Upvotes: 1

Views: 104

Answers (1)

CCCman
CCCman

Reputation: 36

If you can assume maximum length of strings from "assd" child node, you could use System.out.printf. For example:

System.out.printf("%-20s   %s%n", element.getChildNodes().item(0).getNodeValue(), eventy)

would print a value of "assd" child node to first column with width 20 then value of variable eventy and then new line separator.

See Format String Syntax

Upvotes: 2

Related Questions