I cannot get my printf statements to align in java -


i having spot of trouble printf statements. cannot them accurately. code snippet having trouble with.

while(choice != 4){         if(choice == 1){             system.out.println("name            id          gpa\n");             for(j = 0; j < ;j++){                 system.out.printf("%s", fullname[j]);                 system.out.printf("%10d", id[j]);                 system.out.printf("%15.2f\n", gpa[j]);             }             system.out.println("");         } 

it prints this.

name            id          gpa  hoffman caleb joseph         1           4.00 jones bob cray         2           3.80 mitten jose crush         3           1.53 

how can line up?

if add int name format statement align text:

 system.out.printf("%20s", fullname[j]); 

will print

hoffman caleb joseph         1           4.00       jones bob cray         2           3.80    mitten jose crush         3           1.53 

if want left justify put minus in front

 system.out.printf("%-20s", fullname[j]); 

will print

hoffman caleb joseph         1           4.00 jones bob cray               2           3.80 mitten jose crush            3           1.53 

as aside, don't need 3 separate format statements, can combine 3 1:

system.out.printf("%-20s%10d%15.2f%n", fullname[j], id[j], gpa[j]); 

also note new line %n not \n. %n format string print system dependent new line character(s)


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -