3

I have a small piece of code that i don't understand the output.

This output is adding spaces to my string format text. I'm doing something wrong?

public class HelloWorld{

     public static void main(String []args){
        int a1 = 540;
        int a2 = 492;
        int a3 = 200;
        int a4 = 500;
        int a5 = 600;
        String a = "/share.html?title=%1s&description=%2s&image=%3s&width=%4s&height=%5s";
        String b = String.format(a, a1, a2, a3, a4, a5);
        System.out.println(b);
     }
}

The output is:

/share.html?title=540&description=492&image=200&width= 500&height=  600

Why there are those spaces on the last width and height?

1
  • 1
    I think you mean %1$s, %2$s, %3$s, %4$s etc. Or simply %s. Commented Jun 13, 2016 at 13:51

5 Answers 5

6

Remove the numbers from your format specifiers:

String a = "/share.html?title=%s&description=%s&image=%s&width=%s&height=%s";

Using a numerical value inside the format specifier like you did, you are specifying the minimal width of the output - this is the reason why you do not see any spaces for your first three values.

If you want to reference a particular parameter instead of letting the formatter evaluate them in the order they are specified, you can specify an argument index in the form n$ where n is the index of the referenced parameter (like 1$, 2$, ...).

See also java.util.Formatter for more information.

Instead of %s, you could also specify %d since all your arguments are of type int.

Sign up to request clarification or add additional context in comments.

2 Comments

"Java's Formatter does not use position identifiers to reference the respective parameters" By default, yes. But you can specify the argument index.
You should also suggest changing to %d, since the values are int.
2

Because you add Strings with lenght 4 and 5 and your values are ony 3 char.

You shoud use %d for formatting decimal.

The number in front of s is the length not the Position.

Comments

2

The general structure of the format specifier is

%[argument_index$][flags][width][.precision]conversion

If you're trying to refer to argument indexes, you need a $ after the number, e.g.:

height=%5$s

However, since you're only using the parameters once, and in order, you can simply use:

height=%s

Comments

1

%4s means there should be total 4 characters outputted. And your number has 3 characters. So, it displays one " ". So, you need to remove all the numbers from format string.

1 Comment

Not "total", but "at least".
1

try this:

        int a1 = 540;
        int a2 = 492;
        int a3 = 200;
        int a4 = 500;
        int a5 = 600;
        String a = "/share.html?title=%s&description=%s&image=%s&width=%s&height=%s";
        String b = String.format(a, a1, a2, a3,a4,a5);
        System.out.println(b);

let me know if it works

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.