Java string.format() only half working -
i trying construct string using string.format(), 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 run above code, 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 not format last 3 variables?
you made typo's last 3 variables. noted $f
, $d
instead of %f
, %d
.
it should :
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", ...);
Comments
Post a Comment