3

I have this simple method that goes:

private String toJsonFormat(String name, Object value, boolean first) {
    value = value == null ? "" : value; 
    return String.format((first ? "" : ",") + "\"%1s\":\"%2s\"", name, value);
}

When my value argument is null, 2 blank spaces are added after the colon, instead of an empty string.

An example return value when null is passed:

"housenumber":"  "

How come?

1
  • 4
    It would be helpful if you'd provide a short but complete program demonstrating the problem. Also, unless the first part is really required to demonstrate the issue, you should remove that part of it. An ideal question for a problem like this contains a short but complete program with nothing but the issue at hand, along with a description of the expected and actual results. Commented Jan 2, 2015 at 11:45

1 Answer 1

7

The format specifier

%2s

means that this field will be at least two characters wide, space-padded as necessary.

If what you meant is "the second string", then just write

%s

This will automatically give you the second argument because it is the second specifier you use. Same for %1s you have for the first argument.

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

1 Comment

Thank you, yes, I thought it had something to do with the order of the arguments :\

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.