user195257
user195257

Reputation: 3316

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);

All variables are integers,

I want the output to be like

Voter: 1    Candidate: 0     Booth: 1

Thanks

Upvotes: 1

Views: 158

Answers (1)

Joachim Sauer
Joachim Sauer

Reputation: 308031

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);

Upvotes: 10

Related Questions