1

I'm printing out a table format to a text file using Java. I have three columns and three rows. The trick is to get the columns to line up. What's a method of finding the spacing between them? The code below doesn't work because it sets a set spacing between the columns

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class makeTable {

static ArrayList<String> name = new ArrayList<String>();
static ArrayList<String> model = new ArrayList<String>();
static ArrayList<String> year = new ArrayList<String>();

public static void main(String arg[]) {
    addData();
    BufferedWriter writeTable = null;
    try {
        writeTable = new BufferedWriter(new FileWriter("/home/oveledar/Desktop/table.txt"));
        for (int i = 0; i < name.size(); i++){
            writeTable.write(name.get(i) + "   " + model.get(i) +  "   " + year.get(i));
            writeTable.newLine();
        }
    } catch (IOException e) {
        System.err.println(e);
    } finally {
        if (writeTable != null) {
            try {
                writeTable.close();
            } catch (IOException e) {
                System.err.println(e);
            }
        }
    }
}

public static void addData(){
    name.add("Dodge");
    name.add("BMW");
    name.add("VW");
    model.add("Caravan");
    model.add("323i");
    model.add("Passat");
    year.add("2006");
    year.add("2009");
    year.add("2011");

}

Output

Dodge   Caravan   2006
BMW   323i   2009
VW   Passat   2011

"/t" doesn't work for long names

 Dodge  Caravan 2006
 BMW    323i    2009
 VW     Long model name here    2011
ono
  • 2,752
  • 9
  • 38
  • 78
  • For proportional fonts (where padding with spaces will not work), use HTML, and write a ``.
    – Joop Eggen Aug 20 '13 at 15:50
  • possible duplicate of [How can I pad a String in Java?](http://stackoverflow.com/questions/388461/how-can-i-pad-a-string-in-java) – agweber Aug 20 '13 at 15:56

3 Answers3

3

Try padding the strings using String.format()

This question should show you how it works: How can I pad a String in Java?

Pad each of the elements into strings of equal length before you write to the file.

Community
  • 1
  • 1
drewdles
  • 209
  • 1
  • 10
1

Using tabs will not necessarily line up the columns vertically if the data is not close to the same length.

A better solution is to wrap your BufferedWriter with a PrintWriter class and use the printf method.

Something like this:

printf("%-20s%-20s%-20s", name.get(i), model.get(i), year.get(i));

The "-" left justifies the column and 20 is the column width. See the java.util.Formatter javadocs for format specs.

0

Just as you use the new line escape character \n to create a new line, you should include an escape character for a tab, \t after each console write instead of trying to align it with arbitrary spaces.

writeTable.write(name.get(i) + "\t" + model.get(i) +  "\t" + year.get(i));
Daniel B
  • 8,081
  • 5
  • 41
  • 68