简体   繁体   English

在 Java 中加入不同数量空格的字符串

[英]String joining with different number of White Spaces in Java

I'd like to ask if there's a way to use different delimiters or prefixes based on key value of streamed map.我想问一下是否有办法根据流式 map 的键值使用不同的分隔符或前缀。

Here's my code:这是我的代码:

Map<String, Double> averageScoresPerTask...

String summary="\nAverage         |      "+averageScoresPerTask(Stream.of(courseResults.toArray
                    (new CourseResult[0]))).entrySet().stream()
                    .sorted(Map.Entry.comparingByKey())
                    .map(t->String.format(Locale.US,"%.2f", t.getValue())).collect(Collectors.joining(" |                "))
                    +" | "+String.format(Locale.US,"%.2f",averageTotalScore) +" |    "+mark(averageTotalScore)+" |";

What it prints它打印什么

Student         | Lab 1. Figures | Lab 2. War and Peace | Lab 3. File Tree | Total | Mark |
Eco Johnny |56              |69              |90              | 71.67         |    D |
Lodbrok Umberto |70              |95              |59              | 74.67         |    D |
Paige Ragnar |51              |68              |57              | 58.67         |    F |
Average         |          59.00 |                77.33 |                68.67 | 68.33 |    D |

Similar problem with students but my question is regarding Average line so please ignore them.学生也有类似的问题,但我的问题是关于平均线的,所以请忽略它们。 Here's how it should look like.这是它的样子。

Student         | Lab 1. Figures | Lab 2. War and Peace | Lab 3. File Tree | Total | Mark |
Eco Johnny      |             56 |                   69 |               90 | 71.67 |    D |
Lodbrok Umberto |             70 |                   95 |               59 | 74.67 |    D |
Paige Ragnar    |             51 |                   68 |               57 | 58.67 |    F |
Average         |          59.00 |                77.33 |            68.67 | 68.33 |    D |

So the "|"所以“|” are perfectly aligned完全对齐

You can specify the length of your formatted numbers before and after the comma:您可以在逗号前后指定格式化数字的长度:

String.format(Locale.US, "%14.2f", 59.3)

This will make your total formatted number 14 characters long, 2 of that characters will be after the comma, 1 will be the comma itself, that leaves 11 characters for the part before the comma.这将使您的总格式化数字长 14 个字符,其中 2 个字符将在逗号之后,1 个将是逗号本身,留下 11 个字符用于逗号之前的部分。 The number will be right aligned and padded with spaces.该数字将右对齐并用空格填充。

Now you just have to know the length of each heading (you could store it in a list) and adjust that total length for each column.现在您只需要知道每个标题的长度(您可以将其存储在列表中)并调整每列的总长度。 But I think that a stream is not the ideal solution here, I would prefer a loop with an index counter, this way you can access two lists by index (the list of headings or their lengths and the list of values you want to output).但我认为 stream 在这里不是理想的解决方案,我更喜欢带有索引计数器的循环,这样您可以按索引访问两个列表(标题列表或其长度以及要输出的值列表) .

You should align also Strings not only numbers.您不仅应该对齐数字,还应该对齐字符串。 For student the same, student name have different length.对于相同的学生,学生姓名的长度不同。 I would extract the part that calculate averageScores as a String.我会将计算averageScores 的部分提取为字符串。

String averageScores = averageScoresPerTask(Stream.of(courseResults.toArray
        (new CourseResult[0]))).entrySet().stream()
        .sorted(Map.Entry.comparingByKey())
        .map(t->String.format(Locale.US,"%15.2f", t.getValue())).collect(Collectors.joining("|"));

Also don't add spaces on Collectors.joining.也不要在 Collectors.joining 上添加空格。

And than use a single String.format like this:而不是像这样使用单个 String.format :

String.format(Locale.US,"\n%20s|%s| %15.2f|%10s","Average",averageScores, averageTotalScore, mark(averageTotalScore));

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

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