[java] Console Display and Tab Alignment [solved]

Started by
2 comments, last by GameDev.net 18 years, 7 months ago
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]
Advertisement
If you're using Java 1.5, may I suggest System.out.printf()
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!
Use printf, you can then limit the digits printed.

This topic is closed to new replies.

Advertisement