The user types in message and whatever the want in the same sentence.
用户键入消息以及同一句子中的所需内容。
command > message A long message where there cant be more than 56 characters.
I want to take whatever the user types in after "message " and surround it with a box as such:
我想在“消息”之后接受用户输入的任何内容,并用一个框围绕它:
############################################################
# #
# A LONG MESSAGE WHERE THERE CANT BE MORE THAN 56 CHARACTE #
# #
############################################################
The frame should have a width of 60 spaces and atleast a blank space before and after the message. The message can't have a length more than 56 characters.
框架的宽度应为60个空格,并且在消息之前和之后至少应留有空格。邮件的长度不能超过56个字符。
Here's what I tried but I keep getting the error: StringIndexOutOfBoundsException..
这是我尝试但我不断收到错误:StringIndexOutOfBoundsException ..
private void printBoxedMessage() {
String shorterString = nextObjective.substring(8).toUpperCase();
if (!(shorterString.equals(null))) {
for (int n = 0; n < 5; n++) {
if (n == 0 || n == 4) {
for (int m = 0; m < 60; m++) {
System.out.print("#");
}
} else if (n == 1 || n == 3) {
for (int y = 0; y < 60; y++) {
if (y == 0 || y == 59) {
System.out.print("#");
} else if (y > 0 && y < 60) {
System.out.print(" ");
}
}
} else if (n == 2) {
for (int i = 0; i < 60; i++) {
if (i == 0 || i == 59) {
System.out.print("#");
} else if (i == 1 || i == 58) {
System.out.print(" ");
} else if (i == 2) {
System.out.printf("%56s", shorterString.substring(0, 56));
}
}
}
}
}
}
3 个解决方案
#1
1
The way I would handle this is by first checking if the String entered is more than 56 characters. If you don't want it to be, then make the user reenter. Other wise if you want the surrounding areas frame to match the length of what is entered then just create a for loop to match the length of the message entered.
我要处理的方法是首先检查输入的字符串是否超过56个字符。如果您不希望它,请让用户重新输入。另外,如果您希望周围区域框架与输入的长度相匹配,那么只需创建一个for循环以匹配输入消息的长度。
public class PrintBoxedMethod {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Boolean lengthCheck = false;
System.out.println("Enter a message up to 56 characters");
String message = input.nextLine();
while(lengthCheck == false){
if(message.length()> 56){
System.out.println("Message is too long, Enter a message up to 56 characters");
message = input.nextLine();
} else lengthCheck = true;
}
for(int i = 0; i<message.length() + 4;i++){
System.out.print("#");
}
System.out.println();
System.out.print("# ");
System.out.print(message);
System.out.println(" #");
for(int i = 0; i<message.length() + 4;i++){
System.out.print("#");
}
}
}
#2
0
Your code is really hard to understand (at least to me) without any comments to explain stuff. Let's try a more straightforward approach:
你的代码真的很难理解(至少对我来说)没有任何解释的东西。让我们尝试一种更直接的方法:
public static void boxMessage(String message) {
// first line
int length = message.length();
for (int i = 0 ; i < 60 ; i++) {
System.out.print("#");
}
// second line
System.out.print("\n#");
for (int i = 0 ; i < 58 ; i++) {
System.out.print(" ");
}
System.out.print("#\n");
// third line. Left spaces is the number of spaces on the left of the message.
System.out.print("# ");
int leftSpaces = (58 - message.length()) / 2;
for (int i = 0 ; i < leftSpaces ; i++) {
System.out.print(" ");
}
System.out.print(message);
for (int i = 0 ; i < 58 - leftSpaces - length ; i++) { // 58 - leftSpaces - length is number of spaces the right side should have.
System.out.print(" ");
}
System.out.print(" #\n");
// fourth line
System.out.print("#")
for (int i = 0 ; i < 58 ; i++) {
System.out.print(" ");
}
System.out.print("#\n");
// last line
for (int i = 0 ; i < 60 ; i++) {
System.out.print("#");
}
}
Usage:
boxMessage("A LONG MESSAGE WHERE THERE CANT BE MORE THAN 56 CHARACTE");
prints:
############################################################
# #
# A LONG MESSAGE WHERE THERE CANT BE MORE THAN 56 CHARACTE #
# #
############################################################
#3
0
Don't assume the String length. (Correcting formatting) Create an int for size using Math.min(), Then when you get a substring, use the size instead of the 56. For example on your first and last line, what happens if you have a string (nextObjective) shorter than 56 or 8?
不要假设字符串长度。 (更正格式化)使用Math.min()为大小创建一个int,然后当你得到一个子字符串时,使用大小而不是56.例如,在你的第一行和最后一行,如果你有一个字符串会发生什么(nextObjective)短于56或8?
int size = Math.min(incomingString.length,56);
...
System.out.printf("%56s", shorterString.substring(0, Math.min(size,56)));
#1
1
The way I would handle this is by first checking if the String entered is more than 56 characters. If you don't want it to be, then make the user reenter. Other wise if you want the surrounding areas frame to match the length of what is entered then just create a for loop to match the length of the message entered.
我要处理的方法是首先检查输入的字符串是否超过56个字符。如果您不希望它,请让用户重新输入。另外,如果您希望周围区域框架与输入的长度相匹配,那么只需创建一个for循环以匹配输入消息的长度。
public class PrintBoxedMethod {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Boolean lengthCheck = false;
System.out.println("Enter a message up to 56 characters");
String message = input.nextLine();
while(lengthCheck == false){
if(message.length()> 56){
System.out.println("Message is too long, Enter a message up to 56 characters");
message = input.nextLine();
} else lengthCheck = true;
}
for(int i = 0; i<message.length() + 4;i++){
System.out.print("#");
}
System.out.println();
System.out.print("# ");
System.out.print(message);
System.out.println(" #");
for(int i = 0; i<message.length() + 4;i++){
System.out.print("#");
}
}
}
#2
0
Your code is really hard to understand (at least to me) without any comments to explain stuff. Let's try a more straightforward approach:
你的代码真的很难理解(至少对我来说)没有任何解释的东西。让我们尝试一种更直接的方法:
public static void boxMessage(String message) {
// first line
int length = message.length();
for (int i = 0 ; i < 60 ; i++) {
System.out.print("#");
}
// second line
System.out.print("\n#");
for (int i = 0 ; i < 58 ; i++) {
System.out.print(" ");
}
System.out.print("#\n");
// third line. Left spaces is the number of spaces on the left of the message.
System.out.print("# ");
int leftSpaces = (58 - message.length()) / 2;
for (int i = 0 ; i < leftSpaces ; i++) {
System.out.print(" ");
}
System.out.print(message);
for (int i = 0 ; i < 58 - leftSpaces - length ; i++) { // 58 - leftSpaces - length is number of spaces the right side should have.
System.out.print(" ");
}
System.out.print(" #\n");
// fourth line
System.out.print("#")
for (int i = 0 ; i < 58 ; i++) {
System.out.print(" ");
}
System.out.print("#\n");
// last line
for (int i = 0 ; i < 60 ; i++) {
System.out.print("#");
}
}
Usage:
boxMessage("A LONG MESSAGE WHERE THERE CANT BE MORE THAN 56 CHARACTE");
prints:
############################################################
# #
# A LONG MESSAGE WHERE THERE CANT BE MORE THAN 56 CHARACTE #
# #
############################################################
#3
0
Don't assume the String length. (Correcting formatting) Create an int for size using Math.min(), Then when you get a substring, use the size instead of the 56. For example on your first and last line, what happens if you have a string (nextObjective) shorter than 56 or 8?
不要假设字符串长度。 (更正格式化)使用Math.min()为大小创建一个int,然后当你得到一个子字符串时,使用大小而不是56.例如,在你的第一行和最后一行,如果你有一个字符串会发生什么(nextObjective)短于56或8?
int size = Math.min(incomingString.length,56);
...
System.out.printf("%56s", shorterString.substring(0, Math.min(size,56)));