Output in a Table Format in Java's System.Out

Output in a table format in Java's System.out

Use System.out.format . You can set lengths of fields like this:

System.out.format("%32s%10d%16s", string1, int1, string2);

This pads string1, int1, and string2 to 32, 10, and 16 characters, respectively.

See the Javadocs for java.util.Formatter for more information on the syntax (System.out.format uses a Formatter internally).

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

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:

  • Formatting Numeric Print Output (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

  • Formatter (Java Platform SE 8 )

  • PrintStream (Java Platform SE 8 )

Formatting Java Output Like a Table

The error is because%d is for numeric non-floating point values (int, long, etc).

In the line where you print the titles, you have to use %XXs (where XX is a number) since you're passing Strings as parameters:

System.out.format("%10s%15s%15s%15s%20s",
"Grade", "Last Name", "First Name", "Student Number", "Parent Email");

In the line inside the while-loop, you need to set %d for the int and long variables, like Grade and Student Number, there's no need to convert it to String using "" + intProperty:

System.out.format ("%10d%15s%15s%15d%20s",
read.getClass(), read.getLastName(), read.getFirstName(),
read.getStudentNum(), read.getParentEmail());

Since it looks like you want to format the output to the left (and not to the right), you should add a hypen (-) symbol before the XX number:

//similar for title
System.out.format ("%-10d%-15s%-15s%-15d%-20s",
read.getClass(), read.getLastName(), read.getFirstName(),
read.getStudentNum(), read.getParentEmail());

Note: I assumed read.getClass() and read.getStudentNum() would return the Grade and Student number values as int or long.

How to format outputs as a table in Java?

There are various ways to align your output so they align evenly....one way is to replace where you put:

System.out.println(ClassName[i]+"\t \t"+Description[i]+"\t \t"+Units[i]+"\t \t"+Grade[i]);

with:

System.out.printf("%-15s %-15s %-15s ",ClassName[i],Description[i],String.format("%5d", Units[i]),Grade[i]);

instead of using System.out.println() use System.out.printf() that way you can pad the strings and use String.format() inside the printf() function to pad the integer with spaces...you can adjust my padding to whatever you feel fit
this is a more efficient way rather than constantly using tabs

Java output to file in table format?

You can use code like this:

Formatter fmt = new Formatter(); 

System.out.println(fmt.format("%s %s %s %s %s", "Title*", "Title*", "Title*", "Title*", "Title*"));

See Formatter java doc

Sample code which prints table which has 20 characters column width:

    Formatter formatter = new Formatter();
System.out.println(formatter.format("%20s %20s %20s %20s %20s", "Title*", "Title*", "Title*", "Title*", "Title*"));

for (int i = 0; i < 10; i++) {
formatter = new Formatter();
String rowData = "info" + i;
System.out.println(formatter.format("%20s %20s %20s %20s %20s", rowData, rowData, rowData, rowData, rowData));
}

For writing data to file you can use java.io package

Java println formatting so I can display a table?

Yes, since Java 5, the PrintStream class used for System.out has the printf method, so that you can use string formatting.


Update:

The actual formatting commands depend on the data you are printing, the exact spacing you want, etc. Here's one of many possible examples:

System.out.printf("%1s  %-7s   %-7s   %-6s   %-6s%n", "n", "result1", "result2", "time1", "time2");
System.out.printf("%1d %7.2f %7.1f %4dms %4dms%n", 5, 1000F, 20000F, 1000, 1250);
System.out.printf("%1d %7.2f %7.1f %4dms %4dms%n", 6, 300F, 700F, 200, 950);


Related Topics



Leave a reply



Submit