2015-10-08 118 views
0

我一直在试图对齐列中的字符串,但找不到解决方案。 这就是我得到的 - image对齐字符串中的一列java

你可以看到字符串没有对齐。有人可以帮助我调整它们。这是我正在使用的。

String padded = String.format("%-12s  %-12s  %-12s  %-12s  %-12s", currentBalancepadded, radioButtonSelectedPadded, transactionAmountPadded, newBalancePadded, "X"); 
+1

您使用的是固定宽度的字体呢? – CollinD

+0

为什么不把它们放在桌子上? – qingl97

回答

0

您正在为所有字符串使用左对齐。尝试使用右对齐表示金额的字符串。这将正确对齐小数点。

String padded = String.format("%12s %-12s %12s %12s %-12s", currentBalancepadded, radioButtonSelectedPadded, transactionAmountPadded, newBalancePadded, "X"); 

您可能需要调整“12”以获得与标题精确对齐。

+0

是的,我试过,但没有改变任何东西。 –

0

你应该尝试连接你的字符串。要添加一个新行,您应该在要换行的字符串之前连接/n字符。

例如:

public class Test 
{ 
public static void main (String[] args) 
    { 
    String String1 = ""; 
    String String2 = ""; 
    String String3 = ""; 
    System.out.print(String1 + "/n" + String2 + "/n" + String3); 
    } 
} 
+0

这对问题的缩进没有任何帮助。 – csmckelvey