简体   繁体   English

(Java String.format)无法将此列设置为固定宽度

[英](Java String.format) Having trouble making this column a fixed width

First, I apologize if something like this has been answered already. 首先,对于这样的问题是否已经解决,我深表歉意。 I tried to search for any questions like this but I didn't find anything similar (at least with the keywords I used). 我试图搜索类似这样的问题,但没有找到任何类似的问题(至少与我使用的关键字类似)。 Please moderate this as needed! 请根据需要对此进行审核! It's my first post on the site. 这是我在网站上的第一篇文章。

I'm practicing String.format in Java and having trouble with making the elements of my 'Name' column all the same width. 我正在Java中练习String.format,无法使“名称”列的元素都具有相同的宽度。 I'm using JDK 9 if that's relevant at all. 如果所有相关,我正在使用JDK 9。 In my code below, the issue in question is within the for-loop block. 在下面的代码中,有问题的问题在for循环块内。 I'm taking the first and last names from their respective String arrays. 我从各自的String数组中提取名字和姓氏。 I know I can solve my problem if I made each full name into one String, but for the sake of this exercise and in order to further my Java skills, I'd like to know if there is a solution to this problem. 我知道如果将每个全名都放入一个String中就可以解决我的问题,但是为了进行此练习并为了进一步提高Java技能,我想知道是否有解决此问题的方法。 Thank you :) 谢谢 :)

public static void grades() {
    Random rand = new Random();
    // final grade is calculated based on points achieved out of 45. Rounded to 1 decimal
    String[] rosterFirst = {"Harry", "Seamus", "Dean", "Neville", "Ron", "Dennis"};
    String[] rosterLast = {"Potter", "Finnegan", "Thomas", "Longbottom", "Weasley", "Creevey"};

    System.out.println("2018 Hogwarts Graduating Class Scores \n" +
        String.format("%2$-25s %1$-12s", "Grade", "Name"));

    for (int i = 0; i < rosterFirst.length; i++) {
        double finalGrade = (25 + rand.nextDouble() * 20) / 45 * 100;
        System.out.println(String.format("%s, %-15s %-12.1f", rosterLast[i], rosterFirst[i], finalGrade));
    }
}

Output: 输出:

2018 Hogwarts Graduating Class Scores 
Name                      Grade       
Potter, Harry         70.3
Finnegan, Seamus         75.9
Thomas, Dean         96.9
Longbottom, Neville         98.2
Weasley, Ron         58.7
Creevey, Dennis         76.0

(Not sure if code output is wrongly formatted in this post. Please correct me if it is) (不确定本文中代码输出的格式是否错误。如果正确,请更正我)

The current problem is that the last name (first argument) does not have a fixed width. 当前的问题是姓氏(第一个参数)没有固定的宽度。 This will "mess up" the remainder of the string as the width of the last name changes. 当姓氏的宽度改变时,这将“弄乱”字符串的其余部分。 Furthermore, it's not possible to specify an absolute position of additional parameters with String.format alone; 此外,不可能仅使用String.format来指定附加参数的绝对位置。 width formatting only affects relative positions. 宽度格式仅影响相对位置。

An easy way is to get the desired behavior is to specify the full name as a single field with a fixed width, ie: 一种获得所需行为的简单方法是将全名指定为具有固定宽度的单个字段,即:

String fullName = rosterLast[i] + ", " + rosterFirst[i];

System.out.println(
  String.format("%-20s %-12.1f", fullName, finalGrade));

There are now only two arguments actually supplied to the format and both have a fixed width specified. 现在只有两个实际提供给格式的参数,并且指定了固定的宽度。 Having the comma be part of the full name avoids fixed-width interactions between the individual name components and the comma. 将逗号作为全名的一部分可以避免各个名称组件和逗号之间的固定宽度交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM