• Post Reply Bookmark Topic Watch Topic
  • New Topic

Right-justifying text and Formatting Flags  RSS feed

 
Jack Malgam
Greenhorn
Posts: 6
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy,

I am trying to right-justify some text in the console. I have read that the best way to do this is to use String.format(), but every time I use the following code:



I get the following error:



This really irks me, since this code works as expected to left-justify text:



If I'm reading the console output right, the problem is with the "#" flag. I could not find an understandable entry on the Java documentation regarding these flags.

Thanks in advance!
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the documentation for formatter strings is found under java.util.Formatter

If you look under the "Flags" section there, you'll see a table of flags. And, since you're trying to format a String, you want to look at the "Character" column. Which tells you that '-' is an allowable flag for strings, but '#' isn't. '#' can be used for numbers, or for objects that implement Formattable.

I don't know String.format() well enough to know off the top of my head if there is a neat way of right-justifying strings, I'm afraid. Working out the number of spaces to add and printing them yourself is, of course, a possible workaround.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what the '#' flag is supposed to do. But for numeric data, the Formatter API says this:

The padding is on the left by default. If '-' flag is given then the padding will be on the right.


You want right-justified, which means padding on the left. But you used the '-' flag, which puts the padding on the right. So try just removing the '-' flag and see what happens. (No promises: that documentation is too bulky for me as well!)
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at java.util.Formattable , maybe the example will give you an idea. The notes under the Flags section of the java.util.Formatter JavaDocs says "depends on the definition of Formattable" and it doesn't look like java.lang.String implements Formattable. That's probably why you get an exception with the # flag. The example in the Formattable JavaDocs shows how to get right justification. I would "cheat" though and use org.apache.commons.lang.StringUtils for rightPad-ing a String. Nobody ever accused me of being a hard-working programmer in that sense
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This kind of brings back fond memories of a trick I learned from my days of programming xBase:

Edit: Dangit, I just aged myself... "xBase? WTH is that?" the young'uns are probably wondering)
 
Jack Malgam
Greenhorn
Posts: 6
Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot folks, removing the "-" worked like a charm and helped me bum out some unneeded code. Much prefer using this for simple tasks instead of a StringBuffer!

In case anyone looks at this for reference down the line, the final code for right and left padding is:



Hope this helps someone else!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!