Java – 打印文件内容周围的框框

huangapple go评论71阅读模式
英文:

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 &lt;= i; j++) {
   System.out.print(&quot; &quot;);
}         
System.out.print(&quot;|&quot;); */ 

This is the method I'm using:

public static void printBox(Scanner input, int length) {
	
    int top = 0;
    int bottom = 0;
    System.out.print(&quot;+&quot;);
    while (top &lt; length + 2) {
        System.out.print(&quot;-&quot;);
        top++;
    }
    System.out.print(&quot;+&quot;);
    System.out.println();
    
    
    while (input.hasNextLine()) {
        String line = input.nextLine();
        System.out.println(&quot;| &quot; + line);
        
        /* int i = length - line.length();        
        for (int j = 0; j &lt;= i; j++) {
           System.out.print(&quot; &quot;);
        }         
        System.out.print(&quot;|&quot;); */ 
    }
    
    System.out.print(&quot;+&quot;);
    while (bottom &lt; length + 2) {
        System.out.print(&quot;-&quot;);
        bottom++;
    }
    System.out.print(&quot;+&quot;);
}

Here is one test case
printBox(new Scanner(&quot;This is some\ntext here.&quot;), 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.printSystem.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(&quot;+&quot;);
    while (top &lt; length + 2) {
      System.out.print(&quot;-&quot;);
      top++;
    }
    System.out.print(&quot;+&quot;);
    System.out.println();

    while (input.hasNextLine()) {
      String line = input.nextLine();
      System.out.print(&quot;| &quot; + line);

      int i = length - line.length();
      for (int j = 0; j &lt;= i; j++) {
        System.out.print(&quot; &quot;);
      }
      System.out.println(&quot;|&quot;);
    }

    System.out.print(&quot;+&quot;);
    int bottom = 0;
    while (bottom &lt; length + 2) {
      System.out.print(&quot;-&quot;);
      bottom++;
    }
    System.out.print(&quot;+&quot;);
  }

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

  1. 你评论的 for 循环代码非常完美,因为它在正确的垂直线之前添加了额外的空格长度。除了你在 for 循环开始之前使用了 println,这导致空格被添加到下一行而不是连续的位置。

  2. 此外,你可以重复使用上面创建的线来关闭输入,而不是两次使用 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);
}
英文:
  1. 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.

  2. 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(&#39;+&#39;);
    while (top &lt; length + 2) {
        line.append(&quot;-&quot;);
        top++;
    }
    line.append(&quot;+&quot;);
    System.out.println(line);

    while (input.hasNextLine()) {
        String innerLineStr = input.nextLine();
        StringBuilder innerline = new StringBuilder(innerLineStr);
        innerline.insert(0,&quot;| &quot;);

        int i = length - innerLineStr.length();
        for (int j = 0; j &lt;= i; j++) {
            innerline.append(&quot; &quot;);
        }
        innerline.append(&quot;|&quot;);
        System.out.println(innerline);
    }

    System.out.println(line);
}

huangapple
  • 本文由 发表于 2020年9月11日 04:40:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63837392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定