Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setfill() and setw() Java equivalent?

I have an assignment where we are required to create a User class with a toString() method. The toString method should return a string with all the information about the user. We are required to create an array of 50 User objects and print the information about each user on a separate line. The problem is, I want to keep everything in a neat table with all Strings of the same length. So for instance:

User Name          Password          Full Name                     Email
___________________________________________________________________________________________
shortUser          12345             John Smith                    [email protected]
thisUserIsLonger   1234567890        Smitty Werbenjagermanjensen   [email protected]

I would like to keep everything aligned as it is in the above table. This would be easy in C++ since I could just use setw() to dynamically pad spaces between according to the size of the field. Is something like this possible in Java?

like image 783
audiFanatic Avatar asked Dec 17 '25 03:12

audiFanatic


1 Answers

I know this may not be the exact thing you are looking for, but if you were comfortable with printf and its siblings, take a look at the String.format()

like image 74
shinkou Avatar answered Dec 19 '25 17:12

shinkou



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!