arrays - How to make responsive table in Java? -


i beginner in programming , learning how build responsive table in java. came across think need figure out with. let me use easy example illustrate it.

i have 2 arrays,

string[] name = new string[] {"james","tom","rodriguez"}; integer[] score = new double[] {15,20,13}; 

and want transform them this,

name     score james     15 tom       20 rodriguez 13 

i using "\t" , somehow successful in making them aligned when add name bit longer output become this.

name     score james     15 tom       20 rodriguez fernandes  13 

my friend told me use system.out.printf not sure how apply arrays, when data large. me how better format table this?

in order this, first need find string maximum length , append (max length minus string length) spaces while printing other names, e.g.:

public static void print(string[] names, integer[] score){     //find name max length     int maxlength = arrays.stream(names)         .maptoint(string::length)         .max().getasint() + 2;      //now, start printing     system.out.println(string.format("%1$-" + maxlength + "s", "name") + "score");     for(int i=0 ; i<names.length ; i++){         system.out.println(string.format("%1$-" + maxlength + "s", names[i]) + score[i]);     } } 

here, have added 2 max length show 2 empty spaces after name maximum length. can called main, e.g.:

public static void main(string[] args) throws exception{     print(new string[]{"james","tom","rodriguez"}, new integer[]{15,20,30}); } 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -