This question already has an answer here:
- Align printf output in Java 5 answers
for (int j = 0; j < numStu; j++) {
System.out.println((j + 1) + "\t" + StudentName[j] + "\t\t\t" + (Marks[j]) + "\t" + Grade[j]);
}
No. Name Marks Grade
1 ADRIAN TAN 46.00 C-
2 KIM CHEE LIONG HAN 76.00 A-
3 PETER LIM AH MENG 64.00 B-
4 WAYNE WALKER 23.00 F
Sorry for previous question >< This is my output for the command above. The desired output is:
No. Name Marks Grade
1 ADRIAN TAN 46.00 C-
2 KIM CHEE LIONG HAN 76.00 A-
3 PETER LIM AH MENG 64.00 B-
4 WAYNE WALKER 23.00 F
Say X is the length you want, then use
String.format("%-Xs",argument)
. This will cut or extend the string to X length. The - after the % sign says to add the spaces after the string. If you ommit the - sign, it will still work but looks wierd.Here is an example
Edit: changed from
String.format("%-X.Xs",argument)
toString.format("%-Xs",argument)
Hmm do u mean that?:
It should do a thing