user14599899
user14599899

Reputation:

How to print in table format using format() method in JAVA?

I've tried searching for info on "\t", "\n", System.out.printf() and System.out.format() but could not get anything that work.

I'd like to arrange this data into a table with rows and columns.

public static void calc(int upper, int lower) {
    //System.out.print("row       ");
    //System.out.print("aaaa");
    for (int i = 0; lower <= upper; i++) {
        if (i > upper)
            break;
        int square = (int) Math.pow(i,2);
        int cube = (int) Math.pow(i,3);
        double sqrt = (double) Math.sqrt(i);
        System.out.println(square);
    }
}

Upvotes: 1

Views: 1512

Answers (1)

I think you are trying to print output to the console in an organized format that looks like a table and understand that it is not possible to print exactly like a traditional table with row and column, at least not with simple code with String.format() or System.out.printf().

Looking into your code, what I understand is you are trying to print output that will have 4 columns, a number , it's square, it's cube and it's sqrt respectively.

But before going to that I would like to point out one mistake. From your code it looks like you want to find the sqaure, cube and sqrt of a number from range [lower, upper] and your condition for for-loop satisfies that but however your use of i is not relevant. I think you have misunderstood the use of iterative variable i here, the variable i here is unlike any other normal variable just it has different life span (exists only in the for-loop block) and it is changed in the last statement of the for-loop. Futher, I think you do not need this variable i to achieve your goal.

Below I share two ways to achive that goal, using a for-loop and a while-loop.

Using for-loop:

public static void calc(int upper, int lower) {
    System.out.printf("%10s | %10s | %10s | %10s\n", "Number", "Square", "Cube", "Sqrt");
    for (; lower <= upper; lower++) {
        int square = (int) Math.pow(lower,2);
        int cube = (int) Math.pow(lower,3);
        double sqrt = Math.sqrt(lower);
        System.out.printf("%10d | %10d | %10d | %10f\n", lower, square, cube, sqrt);
    }
}

Using while-loop:

public static void calc(int upper, int lower) {
    System.out.printf("%10s | %10s | %10s | %10s\n", "Number", "Square", "Cube", "Sqrt");
    while(lower <= upper) {
        int square = (int) Math.pow(lower,2);
        int cube = (int) Math.pow(lower,3);
        double sqrt = Math.sqrt(lower);
        System.out.printf("%10d | %10d | %10d | %10f\n", lower, square, cube, sqrt);
        lower += 1;
    }
}

Output calc(8, 4) (for both codes above):

Number |     Square |       Cube |       Sqrt

     4 |         16 |         64 |   2.000000

     5 |         25 |        125 |   2.236068

     6 |         36 |        216 |   2.449490

     7 |         49 |        343 |   2.645751

     8 |         64 |        512 |   2.828427

Additional:

If you want to change the right alignment to left alignment then you can just use "-" as shown below.

public static void calc(int upper, int lower) {
    System.out.printf("%-10s | %-10s | %-10s | %-10s\n", "Number", "Square", "Cube", "Sqrt");
    while(lower <= upper) {
        int square = (int) Math.pow(lower,2);
        int cube = (int) Math.pow(lower,3);
        double sqrt = Math.sqrt(lower);
        System.out.printf("%-10d | %-10d | %-10d | %-10f\n", lower, square, cube, sqrt);
        lower += 1;
    }
}

Output calc(8, 4):

Number     | Square     | Cube       | Sqrt      

4          | 16         | 64         | 2.000000  

5          | 25         | 125        | 2.236068  

6          | 36         | 216        | 2.449490  

7          | 49         | 343        | 2.645751  

8          | 64         | 512        | 2.828427 

Note:

  • I used fixed 10 space here since you declared your variables as int and int in JAVA can take upto 10 digits maximum. Exception is sqrt which is a double value and since it is the last value so that will not cause an issue I think.

Reference:

Upvotes: 1

Related Questions