• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

formatted toString with no spacing

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have this code >
in Students class:


in SimpleDB class:


in main class:




the output text is not aligned to the right... does anyone have some ideas how to do it?
 
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeremy,
Welcome to CodeRanch!

Take a look at the System.out.printf method. It lets you specify the desired length/padding/formatting.
 
Jeremy Doseck
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it doesn't work..I tried this code:


my goal is to get rid of brackets and commas that toString() method produces, AND to align all lines to the right..
 
Jeanne Boyarsky
author & internet detective
Posts: 40035
809
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh! Then you want to loop through the ArrayList so only one item is printed per line. That will get rid of the brackets and commas leaving printf to do the alignment.
 
Bartender
Posts: 7197
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You shouldn't use "\n", especially in a printf() where you should use "%n" instead.

In printf, "%s" will print a String using the same number of characters as the String contains.

When you say "justify right" you'd want to pad the string like "%30s". This will allocate 30 characters for the String and display it right justified. To display it left justified use "%-30s". Note that if your string is more than 30 characters it will be printed as wide as nessesary to print all characters in your string, thus throwing off your column alignment.

You can leave spaces or other text between fields in the format string:
 
Jeremy Doseck
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
now it works fine ! thanks.

 
Sheriff
Posts: 15781
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried that using longer names? Like "Bartholomew"? The \t is only going to be consistent if your names are all under 8 characters. Longer names will cause the | and the email address to get pushed out to the next tab stop. You should try short names, too, like Stu, Tim, and Al. Tab stops are typically in multiples of 4 or 8, I can't be sure but it's one of those.
 
To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic