英文:
Java - print box around contents of a file
问题
以下是要翻译的内容:
我正在尝试使用一个接受两个参数的方法来打印文件的内容,这个方法会在内容周围添加一个框,这两个参数分别是:一个代表输入文件的 Scanner 对象,和一个代表该文件中最长输入行的长度的整数。我已经准备好了大部分的框架,但不确定如何在框的右侧放置闭合的竖线。这是我不知道如何修复的部分:
int i = length - line.length();
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
System.out.print("|");
这是我正在使用的方法:
public static void printBox(Scanner input, int length) {
int top = 0;
int bottom = 0;
System.out.print("+");
while (top < length + 2) {
System.out.print("-");
top++;
}
System.out.print("+");
System.out.println();
while (input.hasNextLine()) {
String line = input.nextLine();
System.out.println("| " + line);
/* int i = length - line.length();
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
System.out.print("|"); */
}
System.out.print("+");
while (bottom < length + 2) {
System.out.print("-");
bottom++;
}
System.out.print("+");
}
以下是一个测试用例:
printBox(new Scanner("This is some\ntext here."), 12);
没有 for 循环时,我得到以下结果:
+--------------+
| This is some
| text here.
+--------------+
加入了 for 循环后,我得到以下结果:
+--------------+
| This is some
|| text here.
|+--------------+
英文:
I am trying to print the contents of a file surrounded by a box using a method that accepts two parameters: a Scanner representing an input file, and an integer representing the length of the longest line of input in that file. I have most of the box ready but not sure how to place the closing pipes for the right side of the box. This is the bit I don't know how to fix:
int i = length - line.length();
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
System.out.print("|"); */
This is the method I'm using:
public static void printBox(Scanner input, int length) {
int top = 0;
int bottom = 0;
System.out.print("+");
while (top < length + 2) {
System.out.print("-");
top++;
}
System.out.print("+");
System.out.println();
while (input.hasNextLine()) {
String line = input.nextLine();
System.out.println("| " + line);
/* int i = length - line.length();
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
System.out.print("|"); */
}
System.out.print("+");
while (bottom < length + 2) {
System.out.print("-");
bottom++;
}
System.out.print("+");
}
Here is one test case
printBox(new Scanner("This is some\ntext here."), 12);
Without the for loop, I'm getting the following.
+--------------+
| This is some
| text here.
+--------------+
And this with the for loop
+--------------+
| This is some
|| text here.
|+--------------+
答案1
得分: 1
问题在于你对 System.out.print
和 System.out.println
的使用方式不同。println
在输出末尾添加了一个换行符 \n
。我将你两次打印的函数位置调换后,结果如你所期望。
public static void printBox(Scanner input, int length) {
int top = 0;
System.out.print("+");
while (top < length + 2) {
System.out.print("-");
top++;
}
System.out.print("+");
System.out.println();
while (input.hasNextLine()) {
String line = input.nextLine();
System.out.print("| " + line);
int i = length - line.length();
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
System.out.println("|");
}
System.out.print("+");
int bottom = 0;
while (bottom < length + 2) {
System.out.print("-");
bottom++;
}
System.out.print("+");
}
输出:
+--------------+
| This is some |
| text here. |
+--------------+
另外一个好的提示是,我将 int bottom
的声明和初始化移到了第一次使用它的位置之前。根据 Checkstyle 的规定 VariableDeclarationUsageDistance,这是很好的编码习惯。
英文:
The issue here is the difference between your use of System.out.print
and System.out.println
. println
adds a \n
new line at the end of your output. I switched two of your print functions and it works as desired.
public static void printBox(Scanner input, int length) {
int top = 0;
System.out.print("+");
while (top < length + 2) {
System.out.print("-");
top++;
}
System.out.print("+");
System.out.println();
while (input.hasNextLine()) {
String line = input.nextLine();
System.out.print("| " + line);
int i = length - line.length();
for (int j = 0; j <= i; j++) {
System.out.print(" ");
}
System.out.println("|");
}
System.out.print("+");
int bottom = 0;
while (bottom < length + 2) {
System.out.print("-");
bottom++;
}
System.out.print("+");
}
Output:
+--------------+
| This is some |
| text here. |
+--------------+
Also a good tip, I moved the declaration/initialization of int bottom
to just before its first usage. This is good form based on Checkstyle VariableDeclarationUsageDistance.
答案2
得分: 1
-
你评论的 for 循环代码非常完美,因为它在正确的垂直线之前添加了额外的空格长度。除了你在 for 循环开始之前使用了 println,这导致空格被添加到下一行而不是连续的位置。
-
此外,你可以重复使用上面创建的线来关闭输入,而不是两次使用 top 和 bottom 打印相同的水平线。另外,你可以使用 StringBuilder 来代替过多的 Sysout 和字符串拼接,因为那样不是内存/空间高效的方式。
以下是一个经过优化的代码,包含了上述的建议。
public static void printBox(Scanner input, int length) {
int top = 0;
StringBuilder line = new StringBuilder();
line.append('+');
while (top < length + 2) {
line.append("-");
top++;
}
line.append("+");
System.out.println(line);
while (input.hasNextLine()) {
String innerLineStr = input.nextLine();
StringBuilder innerline = new StringBuilder(innerLineStr);
innerline.insert(0, "| ");
int i = length - innerLineStr.length();
for (int j = 0; j <= i; j++) {
innerline.append(" ");
}
innerline.append("|");
System.out.println(innerline);
}
System.out.println(line);
}
英文:
-
The for loop code that you commented is perfect as it is adding the extra white space to the desired length before the right vertical line.
Except for the fact that you are using println before the for loop starts which is causing the spaces to be added in the next line and not in continuation. -
Also instead of using top and bottom twice to print same horizontal lines. You can use reuse the line created above to be used to close the input.
Additionally you can use StringBuilder instead of using too many Sysout and string concatenation because that is not memory/space efficient.
Below is an optimised code with all the above suggestions.
public static void printBox(Scanner input, int length) {
int top = 0;
StringBuilder line = new StringBuilder();
line.append('+');
while (top < length + 2) {
line.append("-");
top++;
}
line.append("+");
System.out.println(line);
while (input.hasNextLine()) {
String innerLineStr = input.nextLine();
StringBuilder innerline = new StringBuilder(innerLineStr);
innerline.insert(0,"| ");
int i = length - innerLineStr.length();
for (int j = 0; j <= i; j++) {
innerline.append(" ");
}
innerline.append("|");
System.out.println(innerline);
}
System.out.println(line);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论