We help IT Professionals succeed at work.

Variable length in StringBuffer causing columns out of wake

needexpert
needexpert asked
on
309 Views
Last Modified: 2010-03-31
Points are high due to urgency and not necessarily difficulty for the experts out there. Simple basic code is best for me since I am trying to learn from the help I get and not just plugging your answers. Thanks Object... your last help was very simple and easy to understand. I hope you help me here again.


My string array has different length so when I use the loop to

buf.append(multiArray[r][c]+"\t");

in some cases I need \t\t and in others I only need \t in order to line up the columns

how can I either set each column (or incoming array string to fixed width) before appending the next array string to it. For instance an exact number of characters or what ever your suggest.


using a StringBuffer it would be like:

     for(int r=0; r < multiArray.length; r++)
      {
        StringBuffer buf = new StringBuffer();
        for (int c = 0; c < multiArray[r].length; c++)
        {
          buf.append(multiArray[r][c]+"\t");   // append column
        }
        System.out.println(buf); // output row
      }

Comment
Watch Question

Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
u can use the following to pad a string:

String pad(String s, int n)
{
   char[] padding = new char[n - s.length()];
   Arrays.fill(padding, ' ');
   return s + new String(padding);
}

Author

Commented:
I know this wasn't in my original question but I hope you will also answer this question as part of the points offered already.

When I tried to loop to fill my array, I was getting an error and I don't really know why?
Instead of this:
    int row = 4;
    int col = 3;
    String multiArray[][] = new String[row][col];
    multiArray[0][0]= new String("r0c0");
    multiArray[1][0]= new String("r1c0");
    multiArray[2][0]= new String("r2c0");
    multiArray[3][0]= new String("r3c0");
    multiArray[0][1]= new String("r0c1");
    multiArray[1][1]= new String("r1c1");
    multiArray[2][1]= new String("r2c1");
    multiArray[3][1]= new String("r3c1");
    multiArray[0][2]= new String("r0c2");
    multiArray[1][2]= new String("r1c2");
    multiArray[2][2]= new String("r2c2");
    multiArray[3][2]= new String("r3c2");

I coded it like this but it wouldn't populate so I don't know what I was doing wrong?

String[][] arrayOfstrings = {{r0c0,r0c1,r0c3},
                                         {r1c0,r1c1,r1c3},
                                          .......
                                       };


I suggest you finding your string length in every loop and according to its length add tabs as many as you need them.

Author

Commented:
objects:
how does your example fit into the string buffer? where would I put the char[] padding? inside the for loop like and if above the append?

Please show me. Thanks.

Author

Commented:
StillUnAware:
I had previously tried what you suggested by different means of .length < 7, etc. and that did not work so I tried using something like .charAt and if blank than add another /t but that didn't work either.

Author

Commented:
Objects:
I was looking over your example. You are actually doing the fixed length upon filling the array? Am I  Right?  

How would this look different for a multidimetional array?

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Well this code worked for me perfect:

public class StringManipulation {
  public static void main(String[] args) {
    String[][] arrayOfstrings = {{"r0c0","r0c1","r0c3"},
                                 {"r1c0","r1c1","r1c3"}};
    for(int i = 0; i < arrayOfstrings.length; i++)
      for(int j = 0; j < (arrayOfstrings[i]).length; j++)
    System.out.println(arrayOfstrings[i][j]+", ");
  }
}
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
> You are actually doing the fixed length upon filling the array? Am I  Right?

the method returns a fixed length string (according to the length passed as parameter)..

Author

Commented:
Thank you grim_toaster

Does your code mesh within the original code I posted that objects gave me or is it a stand alone?

You are right about the complexity. I can see what it is doing by looking at it by itself but I don't see how the original buf.append which from what I understand is grabing the array no matter what size string it is and just tacking it onto the end of the StringBuffer.

So how does your code mesh with that to insure that it is actually appending the String from the array plus the added padding if needed?  

Thank you for clarifying your example for me.
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Sorry, the last line may be excluded "System.out.println(l);" - it's only the max length retuned by maxStrL function
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you all. I have more than enough to chew on and learn from. Thanks. I will accept all and split points. Thanks again for being so thourough in your help and explanations.
And to integrate the code I provided, simply change:

buf.append(multiArray[r][c]+"\t");   // append column

To instead be:

pad(arrayOfStrings[r][c], buffer, 20);
buf.append('\t');

But I would suggest altering it slightly to be a bit more efficient, this simply create one StringBuffer and appends all data to it (including a line break after each row), and prints out all in one go:
        // initial size to 1000 - a reasonable sized guess...
        StringBuffer buf = new StringBuffer(1000);
        for (int r = 0; r < multiArray.length; r++) {
            for (int c = 0; c < multiArray[r].length; c++) {
                pad(arrayOfStrings[r][c], buffer, 20);
                buffer.append('\t');
            }
            buf.append('\n');
        }

        // output everything in one go
        System.out.println(buf);
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
Thanks, glad I could help.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.