Jump to content

I want to create an output for my app that is always of fixed length. For example,  I have Strings of numbers that look like this: 

01 23 456
789 012 3 4
56 78 9 0 12
34 56 789

 

And I want to change it so it will always look like it's fixed length (like in Word when the text is filled with whitespaces to fill the page):

01   23  456
789  012 3 4
56 78 9 0 12
34   56  789 

 

Any help would be appreciated. 

Link to comment
https://linustechtips.com/topic/927854-streching-string-to-fixed-length-in-java/
Share on other sites

Link to post
Share on other sites

There isn't one correct way because there's more than one way to align the text within each line, but this is how i would approach this.

 

1) pick a max width

2) for each line:
            calculate the total number of white space elements, and divide by (n-1), where n is the number of non-white space groups
            write the non-whitespace groups, and put a number of white spaces between each group using the number above

There are edge cases to consider, (like even/odd line length, a line of all whitespace, a single non-whitespace group, etc.), but that's the general concept.

 

EDIT: actually it'd be simpler to just take the max line length, subtract the length of non-whitespaces, instead of directly counting whitespaces. It's early, I just got to work, and I haven't had any coffee, so there lol.

 

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

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

×