Tuesday, 15 July 2014

In Java can I select a place to begin a line/column when printing? -



In Java can I select a place to begin a line/column when printing? -

for example, want write text file, , want line results columns so:

peanut butter crunchy jelly violet breadstuff wheat milk whole

\t doesn't line sec column when first words different lengths. lining them possible?

yes, possible. want pad strings right white spaces. example, if specify each column starts every 20 characters , string 12 characters, need add together 8 whitespace characters end.

you hand code loop or utilize string.format(). took around online , found easy method can use. http://www.rgagnon.com/javadetails/java-0448.html

public static string padright(string s, int n) { homecoming string.format("%1$-" + n + "s", s); }

s string want pad, n ideal length.

for example,

padright("test", 10") -> "test "

to add together code, format each line. example, first line, do

string line = padright(peanutbutterstring, 20) + peanutbutterattribute

make sure values in array , can loop through , create formatted string.

java printing

No comments:

Post a Comment