lining up data in console output java

自作多情 提交于 2019-12-11 10:15:10

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/31903850/lining-up-data-in-console-output-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!