macaato
macaato

Reputation: 191

Formatting with String.format()

I have to format some data within a binary search tree. I will spare anyone that reads this from actually looking through all my code for the BST (since I don't have an issue with it), so I will simplify it quite a bit. I need to format the Date-Time string part to align. Below is what is currently being outputted for me when I attempt to justify the content to the left, as well as what is outputted when I just attempt to declare the width of the formatting without the "-".

https://i.sstatic.net/0GWrd.jpg

https://i.sstatic.net/EAT7B.jpg

I am posting this to figure out what I am doing wrong and how I can correctly align the date-time string to the left. Thank you for your time and responses

Below are basically the print statements that I have to print out the traversal of my tree, without the actual code of the tree.

System.out.println("CSCI 476 \t 6 \t Computer Security" + String.format("%-20s", "TR-1505-1620"));

System.out.println("CSCI 351 \t 1 \t System Administration" + String.format("%-20s", "TR-1505-1620"));

Upvotes: 1

Views: 66

Answers (1)

FailingCoder
FailingCoder

Reputation: 767

System.out.println("CSCI 476 \t 6 \t Computer Security" + String.format("%-20s", "TR-1505-1620"));

System.out.println("CSCI 351 \t 1 \t System Administration" + String.format("%-20s", "TR-1505-1620"));

"Computer Security".length() == 17; "System Administration".length() == 21; Meaning that System Administration is a longer string, and will show up that way on your console.

What you need to do is check the length of the Strings representing these roles and add or remove spaces accordingly.

Upvotes: 1

Related Questions