2

I have a list with several objects each containing three strings, how should I format the output (in outfile.format()) to make it look like in the sample output below?

public void save() { try { PrintWriter outfile = new PrintWriter (new BufferedWriter (new FileWriter("reg_out.txt"))); for (int i = 0; i < list.size(); i++) { outfile.format("???", list.get(i).getLastName(), list.get(i).getFirstName(), list.get(i).getNumber()); } outfile.close(); } catch (IOException ioe) { ioe.printStackTrace(); } } 

Sample output in reg_out.txt:

Allegrettho Albert 0111-27543 Brio Britta 0113-45771 Cresendo Crister 0111-27440 

1 Answer 1

2

try this

outfile.printf("%-20s%-20s%-20s%n", list.get(i).getLastName(), list.get(i).getFirstName(), list.get(i).getNumber()); 

see java.util.Formatter API for details

Sign up to request clarification or add additional context in comments.

2 Comments

there should be no spaces after numbers, NEW LINE should follow it. Is it possible that getNumber returns a string with a tailing space?
You're correct, I don't what happened, there are no spaces.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.