Java 如何将每个单词设置为新行?

Java 如何将每个单词设置为新行?,java,Java,如何将输入字符串的每个单词设置到新行,并将输出放在ASCII框中 我有以下代码: import java.util.Scanner; public class labSix { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a line of text: "); String line = in.nextLin

如何将输入字符串的每个单词设置到新行,并将输出放在ASCII框中

我有以下代码:

import java.util.Scanner;


public class labSix {
public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.print("Enter a line of text: ");
    String line = in.nextLine();

    printBox(line);

}

// Returns the length of the longest token in the line. 
private static int getMaxLength(String... strings) {
    int len = Integer.MIN_VALUE;
    for (String str : strings) {
        len = Math.max(str.length(), len);
    }
    return len;
}

// Pad the strings with spaces.
private static String padString(String str, int len) {
    StringBuilder sb = new StringBuilder(str);
    return sb.append(fill(' ', len - str.length())).toString();
}

// Fill the string to the amount determined in padString.
private static String fill(char ch, int len) {
    StringBuilder sb = new StringBuilder(len);
    for (int i = 0; i < len; i++) {
        sb.append(ch);
    }
    return sb.toString();
}


// Print the box
public static void printBox(String... strings) {
    int maxBoxWidth = getMaxLength(strings);
    String line = "+" + fill('-', maxBoxWidth + 2) + "+";
    System.out.println(line);
    for (String str : strings) {
        System.out.printf("| %s |%n", padString(str, maxBoxWidth));
    }
    System.out.println(line);
}


}
import java.util.Scanner;
公开课六号{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
System.out.print(“输入一行文本:”);
String line=in.nextLine();
打印盒(行);
}
//返回行中最长标记的长度。
私有静态int getMaxLength(字符串…字符串){
int len=Integer.MIN_值;
for(字符串str:strings){
len=Math.max(str.length(),len);
}
回程透镜;
}
//用空格填充字符串。
私有静态字符串padString(字符串str,int len){
StringBuilder sb=新的StringBuilder(str);
返回某人附加(fill(“”,len-str.length()).toString();
}
//将字符串填充到padString中确定的数量。
私有静态字符串填充(字符ch,整数len){
StringBuilder sb=新StringBuilder(len);
对于(int i=0;i
如果我尝试拆分字符串并将“”替换为“\n”,则在第一行和最后一行中只添加一个“|”


有什么建议吗?

将您的呼叫更改为
printBox
。您当前传递一个名为
line
String
,而不是通过在空格上拆分该行来传递一个
String
(s)数组。像

printBox(line.split(" "));
只有这样的改变,当我输入“Hello world再见world”时,我得到


printBox函数只接收一个包含全部文本的字符串,而不是预期的数组。您可以将其更改为接收数组并拆分接收到的文本以传递给函数,如下所示:

import java.util.Scanner;

public class LabSix {
  public static void main(String[] args) {
      Scanner in = new Scanner(System.in);

      System.out.print("Enter a line of text: ");
      String line = in.nextLine();

      printBox(line.split(" "));

  }

  // Returns the length of the longest token in the line. 
  private static int getMaxLength(String... strings) {
      int len = Integer.MIN_VALUE;
      for (String str : strings) {
          len = Math.max(str.length(), len);
      }
      return len;
  }

  // Pad the strings with spaces.
  private static String padString(String str, int len) {
      StringBuilder sb = new StringBuilder(str);
      return sb.append(fill(' ', len - str.length())).toString();
  }

  // Fill the string to the amount determined in padString.
  private static String fill(char ch, int len) {
      StringBuilder sb = new StringBuilder(len);
      for (int i = 0; i < len; i++) {
          sb.append(ch);
      }
      return sb.toString();
  }


  // Print the box
  public static void printBox(String[] strings) {
      int maxBoxWidth = getMaxLength(strings);
      String line = "+" + fill('-', maxBoxWidth + 2) + "+";
      System.out.println(line);
      for (String str : strings) {
          System.out.printf("| %s |%n", padString(str, maxBoxWidth));
      }
      System.out.println(line);
  }
}
Enter a line of text: test longertest
+------------+
| test       |
| longertest |
+------------+

完美的非常感谢你的帮助。我的代表没有表现出我15岁以下的样子,但你的帖子解决了我的问题。这个解决方案非常有效。谢谢你的回复。
Enter a line of text: test longertest
+------------+
| test       |
| longertest |
+------------+