0
System.out.println("Original Temperature" + "\t Temp in C" + "\t Temp in F" + "\t Temp in K");
        for(int j =0; j < temperatureArray.size(); j++) {
            System.out.println("\t" + df.format(temperatureArray.get(j).getDegrees()) + "\t" + "\t" + "   " + df.format(temperatureArray.get(j).getCelsius())+ "\t" + "\t" + df.format(temperatureArray.get(j).getFahrenheit()) + "\t" + "\t" + df.format(temperatureArray.get(j).getKelvin()));
            

I'm not sure if this is even the proper way to go about table-izing this array but it mostly works. The problem is there are weird non-uniform spaces between elements and I don't know why. I've played around with adding tabs/spaces but nothing is working.

Weird spaces between elements

FTeng
  • 19
  • 2

2 Answers2

0

I'd go with String#format() / System.out.printf():

System.out.printf("%21s %9s %9s %9s\n", 
  df.format(/* ... */), 
  df.format(/* ... */), 
  df.format(/* ... */), 
  df.format(/* ... */));

%9s means: string with a length of 9 characters. If the string is shorter than 9 characters, spaces are appended at the front, so the text is right-aligned. If you want the text to be left-aligned, you can use %-9s.

System.out.printf("%11s | %9s | %9s | %9s\n", "Orig. Temp.", "Temp in C", "Temp in F", "Temp in K");
System.out.printf("%11s | %9s | %9s | %9s\n", "213.0", "99.6", "213.0", "372.7");
System.out.printf("%11s | %9s | %9s | %9s\n", "321.0", "321.0", "609.8", "594.1");

produces:

Orig. Temp. | Temp in C | Temp in F | Temp in K
      213.0 |      99.6 |     213.0 |     372.7
      321.0 |     321.0 |     609.8 |     594.1

(or left-aligned: %-9s)

Orig. Temp. | Temp in C | Temp in F | Temp in K
213.0       | 99.6      | 213.0     | 372.7    
321.0       | 321.0     | 609.8     | 594.1 

(or centered)

private static void print() {
// header
  System.out.printf("%11s | %9s | %9s | %9s\n", "Orig. Temp.", "Temp in C", "Temp in F", "Temp in K");
  // values
  System.out.printf("%-11s | %-9s | %-9s | %-9s\n", center("213.0", 11), center("99.6", 9), center( "213.0", 9), center("372.7", 9));
  System.out.printf("%-11s | %-9s | %-9s | %-9s\n", center("321.0", 11), center("321.0", 9), center("609.8", 9), center("594.1", 9));
}

private static String center(String str, int size) {
  if (str.length() >= size) {
    return str;
  }

  final StringBuilder builder = new StringBuilder();
  for (int i = 0; i < (size - str.length()) / 2; i++) {
    builder.append(' ');
  }
  builder.append(str);
  for(int i = 0; i < (builder.length() - size); i++) {
    builder.append(' ');
  }
  return builder.toString();
}

produces:

Orig. Temp. | Temp in C | Temp in F | Temp in K
   213.0    |   99.6    |   213.0   |   372.7  
   321.0    |   321.0   |   609.8   |   594.1  
Daniel
  • 1,426
  • 1
  • 11
  • 24
  • How would I go about centering it and how do I increase the size of the indent? – FTeng May 11 '21 at 17:40
  • If you want to center the text, take a look at https://stackoverflow.com/a/8155547/14401587 - if you want to increase the size of the indent, just modify the number between `%` and `s`: 9 char indent (right) `%9s`, 4 char indent (left) `%-4s` and so on – Daniel May 11 '21 at 17:43
0

You can use String padding to get each column to be the same number of characters.

String padToRight(String input, String pad, int length) {
    while(input.length() < length) {
        input += pad;
    }
    return input;
}

padToRight("hello", " ", 7) will return "hello "