[java] Console Display and Tab Alignment [solved]

Recommended Posts

Hello, I am just finishing up my Amortization Table when I noticed that since I had to integrate tabbing in my application, my "thrown-together" table will ocassionally go out of alignment due to text exceeding a tab's pre-determined length. Ex. I have System.out.print(Somvar + "\t\t" + Somvar2 + "\t\t" + Somevar3);
100		150		300
100000000000		150		300

If the first or second happen to exceed the tab's length, then all the following are pushed forward and the table alignment is messed up. Is there a way to conteract this in java? I was thinking about converting the variables to strings, counting the characters, and having an if statement that will adjust the number of tabs in relation to how many characters. Thank you. [Edited by - Xiachunyi on September 5, 2005 11:38:13 PM]

Share on other sites
If you're using Java 1.5, may I suggest System.out.printf()

Share on other sites
Thank you for the reply. I asked my professor about the problem as well and he said that my proposed method would work and the "creativity" portion of my grade would be satisfied because of it.

/*==============================================================================Designation: String in Table FormattingUse: Returns a string that complies with the given value length vs. table format==============================================================================*/	public static String DispString(String Characters, int MaxLength)	{		String sCorrectedString;       //Return String Container				if(Characters.length() > MaxLength)		{			sCorrectedString = Characters + "\t";		}		else		{			sCorrectedString = Characters + "\t\t";		}				return sCorrectedString;	}}

Works for me!

Share on other sites
Use printf, you can then limit the digits printed.

Create an account

Register a new account

• Forum Statistics

• Total Topics
627701
• Total Posts
2978709

• 21
• 14
• 12
• 10
• 12