• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

returnString method

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to create a method which returns a formatted String.

For example :

"Field One : "<space><content>
"Field Two : "<space><content>




So all the fields are aligned as if layed out in table format. Whats the best way of inserting the correct space in between the field name and the content?

Thanks in advance
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about the static format method of the String class?
 
Gaz Johnson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankyou for the reply. Maybe i'm being a complete idiot here (quite a high chance in fact) but I had a look at the API earlier and again now and i'm not sure of the method your referring to?
 
Ranch Hand
Posts: 249
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is another formatter available in the java.text package

java.text.MessageFormat.format("This is {0} string", "my");

will return you "This is my string".

Will this suffice ?
 
Gaz Johnson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its in the format

<local string variable><space><string variable derived from a method>

its the space which will vary. I'm guessing theres an easier way than looping through to check for longest string, then setting the space in between the two according to (longest string - lengthOfCurrentString)

So i'm not sure that package would be suitable? Thanks for the reply though, appreciate the help.
 
Gaz Johnson
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Done!

For anyone else searching for same kinda thing :



 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That was the sort of thing I was thinking about, (well done ), but I would suggest some improvements.

If you are using format or anything else which calls Formatter, avoid \n. You are better off with a %n tag, as it says in the Java™ Tutorials here.
Maybe, for performance reasons, try to avoid the + operator. If the format String is too long for the lines, try appending to a StringBuilder, then use the toString() method of StringBuilder to return the final result. StringBuilder is specially designed for rapid appending or insertion.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Maybe, for performance reasons, try to avoid the + operator.



Sorry to correct you twice in one day

It's actually better to use the + operator when concatenating Strings as this allows the compiler to optimise the code.
In Java 1.5 and 1.6 it optimises it by using StringBuilder to build the final string, but if you actually use StringBuilder explicitly then this won't allow later versions of the compiler to use alternative optimisations.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All right, Joanne. Caught me out twice running.
 
reply
    Bookmark Topic Watch Topic
  • New Topic