Java:需要使用 .substring(0,42) 方法打印字符串中的空格



我是编程初学者,但我发现这很容易,直到我的导师希望我们创建一个静态空间(广告牌(并将用户定义的字符串插入该空间。我想出了如何将所有用户的输入作为一个字符串接受。我什至想出了如何在定义的空间内将字符串切成要打印的部分。但是,如果用户输入太短,我会得到一个 OutOfBoundsException。如果字符串太长,它将打印到所有可用的用户定义空间;然后,它再次在定义的空格下方打印整个字符串。

我需要知道如何接受空白空间。我还需要知道如何对打印的字符进行限制。

这是代码:

//this is a subclass of the Billboard superclass
package itsd322_u4_ip;
import java.util.Scanner;
public class Create extends Billboard {
Scanner scan = new Scanner(System.in);
String userInput = "";
{
    System.out.println("Enter your message [0-336 caharacters]:n");
    userInput = scan.nextLine();

    System.out.println("**********************************************");
    System.out.println("*                                            *");
    System.out.println("* Your message was:                          *");
    System.out.println("*                                            *");
    System.out.println("* " + userInput.substring(0,42) +          " *");
    System.out.println("* " + userInput.substring(42,84) +         " *");
    System.out.println("* " + userInput.substring(84,126) +        " *");
    System.out.println("* " + userInput.substring(126,168) +       " *");
    System.out.println("* " + userInput.substring(168,210) +       " *");
    System.out.println("* " + userInput.substring(210,252) +       " *");
    System.out.println("* " + userInput.substring(252,294) +       " *");
    System.out.println("* " + userInput.substring(294,336) +       " *");
    System.out.println("*                                            *");
    System.out.println("**********************************************");
}
@Override
public void displayInfo()
{
    System.out.println(userInput);
    System.out.println();
}
}
for (int i = 0; i < userInput.length(); i += 42) {
    System.out.printf("* %-42s *n", userInput.substring(i, Math.min(i + 42, userInput.length())));
}

@ScottRLemon的建议可确保substring的起始索引始终有效。但是,您还必须对结束索引执行相同的操作(这就是Math.min调用在此处所做的(,并且为了美观起见,您必须确保给定的行具有 42 个字符,即使打印了较短的部分(可以 - 并且通常确实 - 发生在字符串的末尾(。格式字符串 %s 用于字符串,%42s 用于 42 个字符,%-42s 用于 42 个字符,其中较短的字符串向左对齐。
printf打印,这里没关系。但是,如果您需要存储结果,String.format可以这样做:

String somestring=String.format("%-42s", "Hello");


保持电路板尺寸的一种方法:
int i=0;
while(i < userInput.length()){
    System.out.printf("* %42s *n", userInput.substring(i, Math.min(i + 42, userInput.length())));
    i += 42;
}
while(i < 336){
    System.out.println("*                                            *");
    i+=42;
}

也许而不是

System.out.println("* " + userInput.substring(0,42) +          " *");
System.out.println("* " + userInput.substring(42,84) +         " *");
System.out.println("* " + userInput.substring(84,126) +        " *");
System.out.println("* " + userInput.substring(126,168) +       " *");
System.out.println("* " + userInput.substring(168,210) +       " *");
System.out.println("* " + userInput.substring(210,252) +       " *");
System.out.println("* " + userInput.substring(252,294) +       " *");
System.out.println("* " + userInput.substring(294,336) +       " *");

您可以使用 for 循环遍历所有可能的子字符串。这样:

for(int i = 0; i < userinput.length()/42 + 1; i++){
System.out.println("* " + userInput.substring(42*i, 42*i + 42) + " *"); 
}

这样,字符串就可以随心所欲地长。

最新更新