开发者

String formatting problem Java

im trying to format this string into a fixed column style but cant get it to work, heres my code, whats up?

System.out.format("%32s%10n%32s%10n%32s%10n", "Voter: " + e.voteNo + "Candidate: " + vote + "Booth: " + boothId);开发者_Go百科

All variables are integers,

I want the output to be like

Voter: 1    Candidate: 0     Booth: 1

Thanks


Please stop trying to program by accident. Your code looks like the glued-together parts of 3 different approaches to solving the issue. Try to read the documentation on the topic (JavaDoc is your friend!) and apply what you learned instead.

String result = String.format("Voter: %-10d Candidate: %-10d Booth: %-10d", e.voteNo, vote, boothId);
System.out.println(result);

For more information on String.format check its JavaDoc.

Edit: apparently I didn't get the memo that there's actually a PrintStream.format, so you can actually write it like this:

System.out.format("Voter: %-10d Candidate: %-10d Booth: %-10d", e.voteNo, vote, boothId);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜