Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Truncating/ Padding a string

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

I'm having some trouble creating a method (in the form.....
)

that positions string s in a field of a desired width. If the string is longer than the desired field width, it will need to be truncated. If it is shorter than the desired field width it will need to be padded with blank characters. The alignment parameter determines whether the string will be left aligned ('L' or 'l') in the field (padding added to the right) or right aligned ('R' or 'r') in the field (padding added to the left). And if there are not enough spaces to display the string, at least one space must be provided on the appropriate side.

I've been playing around with it:



And I've been testing it in various ways. But ultimately the I/O should look similar to this:


I've been struggling with this for quite some time now, so any help would be greatly appreciated.

Thanks,

n
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it might be helpful for you to describe the steps you want to follow (your algorithm) in English before trying to code it in Java. Once you have the logical steps clear, it will be much easier to code.

For example, you might say something like this:
1) Determine whether the String will fit in the given space.
2) If it will fit, then...
3) If it will not fit, then...
etc.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course, you are not allowed to use String.format ?
 
Marshal
Posts: 79634
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Christophe Verre:
Of course, you are not allowed to use String.format ?



Or, it might be better still to use a StringBuilder.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic