简体   繁体   English

用Java格式化列输出

[英]Formatting column output in Java

I have the below output from a loop in java, I would like to know as to how to set the gap between the columns so the output looks neat. 我从java中的循环中获得以下输出,我想知道如何设置各列之间的间隙,以便输出看起来整洁。

Name[Last,First]:Cruise, Tom     Movie:MI    Year:1990s     
Name[Last,First]:Hanks, Tom     Movie:Cast Away     Year:2000s   

I want the Movie and year to be aligned along the same line. 我希望电影和年份沿同一直线对齐。 I have used three methods getName() , getMovie, getYear() to obtain data. 我使用了三种方法getName(),getMovie和getYear()来获取数据。

try to use the tab character, If i remember right it will make it align nicely 尝试使用制表符,如果我没记错的话,它会很好地对齐

\\t \\ t

or Learn the printf, which is a shortcut for String.format(); 或学习printf,这是String.format()的快捷方式;

System.out.printf("%12s%12s%n",hand1,h…

% = entity %=实体

s = String var placeholder s =字符串var占位符

12 = spaces 12 =空格

%n = system independent line-end return %n =与系统无关的线路端返回

尝试使用String类的format()或printf()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM