0

I am trying to construct a string using string.format(), but it stops working (just over) half way through.

private String constructMessage(String messageType, int messageCode,Position location) {
    double lat = location.getLat();
    double lon = location.getLon();
    double ele = location.getEle();
    double speed = 0.0;
    double heading = 0.0;

    String msg = "$PNGVEST," + messageType + "," + VEST_ID + ","
            + System.currentTimeMillis() + "," + lat + "," + lon + "," + ele
            + "," + speed + "," + heading + "," + messageCode;

    String test = String.format("$PNGVEST,%s,%d,%d,%f,%f,%f,$f,$f,$d",
            messageType, VEST_ID, System.currentTimeMillis(), lat, lon, ele,
            speed, heading, messageCode);

    System.out.println(msg);
    System.out.println(test);
    return msg;
}

When I run the above code, I get the following output:

$PNGVEST,LOCATION,1,1439998967553,51.558641765246364,-3.043043462354341,-1.0,0.0,0.0,0
$PNGVEST,LOCATION,1,1439998967553,51.558642,-3.043043,-1.000000,$f,$f,$d
$PNGVEST,LOCATION,1,1439998968553,51.55862560874394,-3.043027770590568,-1.0,0.0,0.0,0
$PNGVEST,LOCATION,1,1439998968553,51.558626,-3.043028,-1.000000,$f,$f,$d

Why does it not format the last 3 variables?

2 Answers 2

4

You made typo's with your last 3 variables. You noted $f and $d instead of %f and %d.

It should be :

String test = String.format("$PNGVEST,%s,%d,%d,%f,%f,%f,%f,%f,%d",
        messageType, VEST_ID, System.currentTimeMillis(), lat, lon, ele,
        speed, heading, messageCode);

Instead of

String.format("$PNGVEST,%s,%d,%d,%f,%f,%f,$f,$f,$d", ...);
Sign up to request clarification or add additional context in comments.

2 Comments

What a fresh set of eyes can do. It even took me about a minute to notice what you had even changed.
Such typo's are indeed hard to see, however the $ signs in the output should have alarmed you ;)
0

try to change,

String test = String.format("$PNGVEST,%s,%d,%d,%f,%f,%f,$f,$f,$d",
        messageType, VEST_ID, System.currentTimeMillis(), lat, lon, ele,
        speed, heading, messageCode);

to

String test = String.format("$PNGVEST,%s,%d,%d,%f,%f,%f,%f,%f,%d",
        messageType, VEST_ID, System.currentTimeMillis(), lat, lon, ele,
        speed, heading, messageCode);

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.