Java class with string and given length

I am creating a java program that creates a file and populates it with data in string form with each field in the record limited to a specific length.

It seems to me that I need to create a class that has a string for the data field and a length that defines the length of the string. And then somehow constrain it so that the string never exceeds that length, either by swapping, padding with spaces (if the input is less than the length), or trimming the end to make the data fit.

My first question is if there is already a base class that will do this, or should I just roll my own.

My second question is, if I am actually folding myself, how can I make sure when the class is built that the initial value of the inner string is less than the specified length, Ive read elsewhere that the constructor should return a value, but is there really an exception for the constructor ?

Thanks for any help.

+2


source to share


4 answers


Here's my take - it meets all the requirements in your question. You will notice that I decided to reject illegal assignments with IllegalArgumentException

.



package com.pragmaticsoftwaredevelopment.stackoverflow;

public class FixedLengthString {
   private String string = null;
   private int length = -1;
   public FixedLengthString (int length) { 
      this.length = length; 
   }
   public void setValue (String str) {
      if (str.length ()> length)
         throw new IllegalArgumentException ("ERROR: Assigned string length (" + str.length () + ") exceeded given limit (" + length + ")");
      else
         this.string = str;
   }
   public String toString () {
      return String.format ("% 1 $ -" + length + "s", string);

   }
   public String getString () { 
      return toString ();
   }
}


+3


source


Use printf or format



System.out.printf("\n%-5d%-20s",10,"Hello");

      

+2


source


You can use StringUtils which is part of Commons Lang and use the following method:

String rightPad(java.lang.String str, int size, char padChar) 

      

like this:

String myPaddedString = StringUtils.rightPad("Sample string", 20, "*") 

      

Then you can write this to a file - take a look at Commons IO FileUtils for a neat way of writing a string easily ...

At your second point .. the constructor cannot return a value, otherwise it will be a standard method, but it can raise an exception based on the parameters passed into it - an IllegalArgumentException is most appropriate in this case.

+2


source


You can do something like this:

public class Field {
    // other members not shown
    int length;
    String value;

    public Field(int length, String initialValue) {
        // omitted error check for length > 0
        this.length = length;
        setValue(initialValue);
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        if (value != this.value) {
            if (value == null)
                value = "";
            if value.length() > length)
                value = value.substring(0, length);
            else
                value = String.format("%1$-" + length + "s", value);
            this.value = value;
        }
    }
}

      

0


source







All Articles