[Solved] How to add a line with this sign “|” in between every column in java program ? What is the logic for loop? [closed]


Its simple:
Let’s assume your each column is 15 characters wide, if it’s less or more change accordingly:

// Format header
System.out.println(String.format("%1$-15s", "EMPO" )+ "|"+"\t"+String.format("%1$-15s", "ENAME" )+ "|"+"\t"+String.format("%1$15s", "SAL" )+ "|"+"\t"+String.format("%1$15s", "AVERAGE" )+ "|");

Similarly format your row: I leave this one for you to practice.

System.out.println(eno+"\t"+ename+"\t"+sal+"\t"+avg);

If you are not able to figure out, let me know.

Explanation:

String.format("%1$-15s", "EMPO")

This is for strings. It will do right padding, to make it 15 characters. So if you string is less than 15 characters, it will append ' ' space characters. This is done to maintain column width on console output.

"|"

This is simple, it just adds pipe characters

String.format("%1$15s", "SAL" )

This one is for numbers, as numbers need to be right aligned.

solved How to add a line with this sign “|” in between every column in java program ? What is the logic for loop? [closed]