1

I want to print variables in two columns, but I need to nicely print them. What I have is:

Imagine var1 and var2 are string that will be change according to user Input throughout the program. The length of var1 will affect the position of var2, so the position of var2 will not be parallel. So is there a java method that can customize the print format?

What I want: I want to make sure that both columns will be listed in parallel manner regardless the length of them

What I hace tried so far: System.out.printf("%s %13s\n", var1, var2);

1)apple   1)bad fruit
2)orange  2)good fruit
3)pig      3)animal <----- off
ZpfSysn
  • 807
  • 2
  • 12
  • 30
  • You need java's Formatter. https://docs.oracle.com/javase/7/docs/api/java/util/Formatter.html – franklin May 21 '18 at 00:01
  • 1
    Possible duplicate [Writing data to text file in table format](https://stackoverflow.com/questions/26229140/writing-data-to-text-file-in-table-format/26229246#26229246) – MadProgrammer May 21 '18 at 00:20

1 Answers1

2

Like in C, there is a printf function. You can pass it %<num>s which will print a given string taking up exactly <num> characters. If length of the string is less than <num>, spaces will be added. So first argument is format in which you want to print and other argument are values which will be formatted. For example, as format you can specify %d which will print integer, %f which will print float, %s which will print string, etc.

System.out.printf("%8s %7s %6s\n", "One", "Two", "Three");

Here is the code that solves your problem:

    String[][] values = {
        {"apple", "good fruit"},
        {"orange", "bad fruit"},
        {"pig", "animal"}
    };

    int maxLength = 0;

    for(int i = 0; i < 3; i++) {
        for(int j = 0; j < 2; j++) {
            if(values[i][j].length()> maxLength) {
                maxLength = values[i][j].length();
            }
        }
    }

    String format = "%d)%-" + maxLength + "s %d)%-" + maxLength + "s\n";

    for(int i = 0; i < 3; i++) {
        System.out.printf(format, i + 1, values[i][0], i + 1, values[i][1]);
    }

This is the output of the code above:

1)apple      1)good fruit
2)orange     2)bad fruit 
3)pig        3)animal 
jelic98
  • 723
  • 1
  • 12
  • 28
  • May you explain how does above code work? what does each argument do. since I am not sure how do i apply the code to my print statement – ZpfSysn May 22 '18 at 07:02
  • I've edited the answer that covers your problem. You can take a look at full tutorial here: https://www.tutorialspoint.com/c_standard_library/c_function_printf.htm – jelic98 May 22 '18 at 12:04
  • i still cant get it to work. i am sorry formatting is really not making much sense to it and im grabbing it really slow. What I have tried so far System.out.printf("%30", k + ")", j + ")" + var1 + "s" , j + ") " + var2);. so the 30 determines the number of length of the whole string, and fill in with "s" which is space between them, but it ran into unknownformatconversionexception – ZpfSysn May 22 '18 at 15:04
  • What you have is different from what I want. I want the output to be `"1)Var 1 . 1)Var2"` . Since length of Var 1 will affect the placement of Var2, I want to make sure that regardless how long Var1 is, the column of var2 is always parallel. So i essential it is a string consist of two smaller string which is `"i + ")"` + Var1" and `"i +")" + Var2` – ZpfSysn May 22 '18 at 15:09
  • What I have `System.out.printf("%s %13s\n", var1, var2);` The first column is fine, the second column is still not parallel – ZpfSysn May 22 '18 at 22:27
  • I've noticed that you unchecked my answer so I've edited it. Length of var1 should not be greater than 30. – jelic98 May 22 '18 at 22:30
  • Can you explain why the line i have won't work? it looks really similar to your modified answer – ZpfSysn May 22 '18 at 22:34