Quantcast

Maximum PC

It is currently Wed Apr 16, 2014 6:35 pm

All times are UTC - 8 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Formatting Output in Java
PostPosted: Sat Oct 24, 2009 1:41 pm 
Northwood
Northwood
User avatar

Joined: Mon Sep 17, 2007 1:27 pm
Posts: 3311
Location: Toronto, ON
Is there a way to format output in java to make it exactly 4 numbers long?

For example, if you output "12", it make it " 12", and if you output "1234", it'll output "1234". Basically, it'll add spaces in front to make the total 4 numbers long.


Top
  Profile  
 
 Post subject: Formatting Java strings
PostPosted: Wed Oct 28, 2009 8:27 pm 
8086
8086

Joined: Sun Apr 12, 2009 9:14 am
Posts: 13
There are likely a million ways to do this, but how about writing a class that does it for you?

Code:
public class FourCharString {
   private static final int FORMAT_MASK = 4;
   private static final char PAD_CHARACTER = ' ';
   private String theString;
   
   /**
    * This constructor accepts a String. If the characters
    * in the String exceed 4, the subsequent characters
    * are truncated.
    * @param theString
    */
   public FourCharString(String theString){
      if(theString.length() > FORMAT_MASK){
         theString = theString.substring(0, FORMAT_MASK);
      }
      this.theString = theString;
   }
   
   public String toString(){      
      StringBuffer sb = new StringBuffer(theString);
      
      int count = FORMAT_MASK - theString.length();
      
      while(count-- > 0){
         sb.insert(0, PAD_CHARACTER);
      }
      
      return sb.toString();
   }
   
   public static void main(String[] args){
      int initialValue = 11;
      
      FourCharString fcs = new FourCharString("" + initialValue);
      System.out.println(fcs);
   }
}


The main is there for testing purposes only, of course.

Gadget: I added the code tags.


Top
  Profile  
 
 Post subject:
PostPosted: Thu Oct 29, 2009 7:10 am 
Million Club - 5 Plus*
Million Club - 5 Plus*
User avatar

Joined: Sun Sep 12, 2004 6:37 pm
Posts: 4740
Location: In the monkey's litterbox
Code:
String.format("%4f", doubleNumber);
String.format("%4s", aString);


String.format is very handy, it uses printf syntax.

String.format documentation


Top
  Profile  
 
 Post subject: String.format
PostPosted: Sat Oct 31, 2009 10:38 pm 
8086
8086

Joined: Sun Apr 12, 2009 9:14 am
Posts: 13
smartcat99s wrote:
Code:
String.format("%4f", doubleNumber);
String.format("%4s", aString);


String.format is very handy, it uses printf syntax.

String.format documentation

It looks like the '-' character can enforce the left-justified need. Nice call. Check the Java version you are using to ensure that String.format is available.


Top
  Profile  
 
 Post subject: Re: Formatting Java strings
PostPosted: Fri Nov 20, 2009 7:05 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
jasonfactor wrote:
There are likely a million ways to do this, but how about writing a class that does it for you?


Let's assume that the functionality wasn't offered in the Java API. We have a couple of choices regarding how to "design" this class. I'm going to offer you some tips on how to make this class a bit more useful, and hopefully, we'll both come away with a better appreciation on how to create a more general design (and I realize that you were probably thinking more in terms of just pumping this out for a class assignment -- or for someone else to do so).

OK, the first thing that I would think if I saw this class in the API is why does it force me to use only 4 characters and why can't I pad with something besides a space? You could obviously add the two additional parameters to your constructor for the pad character and the pad length OR add a second constructor leaving the defaults as a space and 4.

BTW, I'm going to ignore the truncation rule that you introduced. This class will just do "padding' for the moment. If you want to truncate stuff, you should probably create a separate class. And we should probably rename the class to something more appropriate like FormatString.

Code:
   public FormatString(String theString){
      this.theString = theString;
   }

   public FormatString(String theString, char padCharacter, int length) {
      this.theString = theString;
      FORMAT_MASK = length;
      PAD_CHARACTER = padCharacter;
   }


Good -- the class is already more useful and all we've done so far is add a constructor. =)

The next thing to consider is the "how is someone going to use this class" sort of thought. I don't see people really wanting to instantiate an object every time they want to format a string. In all likelihood, they're going to use the object only one time and move on and need to format another string.

For example, I might have an array of integers. Do I really want to write something like...

Code:
for (int i = 0; i < arr.length; i++)
  new FormatString(arr[i], pad, len).toString();


If the array contained a 1000 integers, I just allocated memory for the pad length 1000x, the pad char 1000x, etc. This is one of those cases where a static methods are probably more useful than a standard class method.

Code:
public class FormatString {
   
   public formatString(String s, char padChar, int length) {

      StringBuffer sb = new StringBuffer(s);
      int padLength = length - s.length();

                for (int i = 0; i < padLength; i++)      
         sb.insert(0, padChar);
      
      return sb.toString();
   }
}


Looks pretty good to me. What do you think?


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group