1

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

1 Answer 1

10

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);
Sign up to request clarification or add additional context in comments.

1 Comment

@sje397: yes, I just realized. The glueing-together part still applies, as he's mixing string concatenation with format and wrong format definitions (%n is a newline in Java and not a numeric format).

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.