2015-10-08 158 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