0

I'm learning Java and have spent way too much time on this stupid little problem. I'm trying to dynamically pad the left side of my string outputs with spaces, so all values displayed will be padded left. The problem is, I don't know the length of the values until a user enters them.

Here's an example of what I'm trying to do. nLongestString is the length of the longest string I'm displaying, and strValue is the value of the string itself. This doesn't work dynamically at all. If I hardcode a value for nLongestString it works, but I can't do that since I don't always know how long the strings will be.

 System.out.printf("%"+nLongestString+"s", strValue + ": ");

Output should look like:

thisisalongstring:
       longstring:
            short:
5
  • Can't you store values from user and print them after you have all of them? Commented Oct 12, 2013 at 22:32
  • Yes, that's what I'm doing, but I don't see how that changes anything. Commented Oct 12, 2013 at 22:34
  • If you can store users data before printing it you can iterate over them once looking for max length and then iterate second time printing it. Commented Oct 12, 2013 at 22:37
  • Yeah, that's actually what I'm doing. Commented Oct 12, 2013 at 22:45
  • Deleted my answer because either I'm tired or something odd was happening with the way this page got rendered. As izmaki pointed out. Commented Oct 12, 2013 at 22:50

2 Answers 2

1

I'm not seeing your problem, the following works fine for me. (Java 7)

Edit: Have you checked the value of nLongestString? I'm guessing it doesn't get set to what you think it does.

    String[] arr = { "foo", "bar", "foobar" };

    int max = 0;

    for( String s : arr ) {
        if( s.length() > max ) {
            max = s.length();
        }
    }

    for( String s : arr ) {
        System.out.printf(  ">%" + max + "s<%n", s );
    }

    Random random = new Random( System.currentTimeMillis() );
    // just to settle the question of whether it works when 
    // Java can't know ahead of time what the value will be
    max = random.nextInt( 10 ) + 6;

    for( String s : arr ) {
        System.out.printf(  ">%" + max + "s<%n", s );
    }
}

Output:

>   foo<
>   bar<
>foobar<
// the following varies, of course
>     foo<
>     bar<
>  foobar<
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, thanks. I was doing the exact same thing, only my 's' value from your example was appending some addition chars to it. Once I took those chars into account, everything works fine.
0

If you already have your data then you just need to find max length of your words and after that print them. Here is code sample

// lets say you have your data in List of strings
List<String> words = new ArrayList<>();
words.add("thisisalongstring");
words.add("longstring");
words.add("short");

// lets find max length
int nLongestString = -1;
for (String s : words)
    if (s.length() > nLongestString)
        nLongestString = s.length();

String format = "%"+nLongestString+"s:\n";// notice that I added `:` in format so 
                                        // you don't have to concatenate it in 
                                        // printf argument

//now lets print your data
for (String s:words)
    System.out.printf(format,s);

Output:

thisisalongstring:
       longstring:
            short:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.