Sign in to follow this  
Xiachunyi

[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 this post


Link to post
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 Formatting
Use: 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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Use printf, you can then limit the digits printed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this