How to print align with my display [duplicate]

好久不见. 提交于 2019-12-01 15:31:32

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

String[] name = {"Ben", "Jacob", "Tyler"};
        int[] age = {5, 16, 25};
        int[] salary = {0, 1000, 100000};
        System.out.printf("No.\t%-10s\t%-10s\t%-10s\n", "Name", "Age", "Salary");
        for (int i = 0; i < name.length; i++) {
            System.out.println(String.format("%-3d\t%-10s\t%-10d\t%-10d", i, name[i], age[i], salary[i]));
        }

Edit: changed from String.format("%-X.Xs",argument) to String.format("%-Xs",argument)

Hmm do u mean that?:

for (int j = 0; j < numStu; j++) {  
System.out.println("\n" + (j + 1) + " " + StudentName[j] + "\t" + (Marks[j]) + "\t" +     Grade[j]);}  

It should do a thing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!