Java-在文件内容周围打印框

问题描述

我正在尝试使用一种接受两个参数的方法来打印由框包围的文件内容:代表输入文件的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.
   |+--------------+

解决方法

这里的问题是您使用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的好形式。

,
  1. 您评论过的for循环代码是完美的,因为它在右垂直线之前将多余的空白添加到所需的长度。 除了您在for循环开始之前使用println之外,这会导致在下一行而不是连续添加空格。

  2. 也不要两次使用顶部和底部来打印相同的水平线。您可以重复使用上面创建的行来关闭输入。 另外,您可以使用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);
}