英文:
Is there a way to make a string from ArrayList elements?
问题
我想要求用户输入一条消息:
> "按以下格式输入消息:"TO FROM MESSAGE : KEY""
用户输入示例:
> 1 2 嗨,你好吗?: bubble123
然后,我想让SecureMessage
类返回这条消息。然而,我不知道如何创建消息部分的字符串,因为getMessage()
方法需要一个分配给它的字符串。
SecureMessage
类不应该有任何修改器。SecureMessage
中的所有字段都从Message
类中设置。
public class SecureMessage {
private String message;
private String sourceName;
private String destName;
private String key;
public SecureMessage(String message, String sourceName, String destName, String key){
this.message = message;
this.sourceName = sourceName;
this.destName = destName;
this.key = key;
}
public String getMessage(String key){
return message + " 来自SecureMessage类";
}
public String getSourceName(){
return sourceName + " 来自SecureMessage类";
}
public String getDestName(){
return destName + " 来自SecureMessage类";
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Message {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String message = "空白", sourceName = "空白", destName = "空白", key = "空白", messageInput, userMessageDetails;
ArrayList<String> messageInputList = new ArrayList<>();
System.out.println("按照格式 TO FROM MESSAGE : KEY 输入消息\n示例 --- 1 2 嗨,你好吗?: bubble123");
userMessageDetails = keyboard.nextLine();
String str = userMessageDetails;
String[] tokens = str.split(" ");
for(String s : tokens){
messageInputList.add(s);
}
System.out.println(messageInputList);
System.out.println("");
int x = messageInputList.size();
destName = messageInputList.get(0);
sourceName = messageInputList.get(1);
key = messageInputList.get(x-1);
//--这是我迷失的地方,因为我需要向SecureMessage的getMessage()方法传递一个字符串---
// message = ???;
// 我找到了如何打印这些项,但这不是一个字符串...
// for(int i = 2; i <= (x-3); i++){
// System.out.print(messageInputList.get(i) + " ");
// }
//------------------------------------------------------------------------------------------------------
SecureMessage secMsg = new SecureMessage(message, sourceName, destName, key);
System.out.println(secMsg.getDestName());
System.out.println(secMsg.getSourceName());
System.out.println(secMsg.getMessage(key));
}
}
英文:
I want to ask the user for a message:
> "Enter a message in the following format: "TO FROM MESSAGE : KEY"
Example input from user:
> 1 2 hey there how are you : bubble123
Then, I want to have the SecureMessage
class return the message. However, I do not know how to create a String of the message part, as the getMessage()
message requires a String assigned to it.
The SecureMessage
class should not have any mutators in it. All fields in SecureMessage
are set from the Message
class.
public class SecureMessage {
private String message;
private String sourceName;
private String destName;
private String key;
public SecureMessage(String message, String sourceName, String destName, String key){
this.message = message;
this.sourceName = sourceName;
this.destName = destName;
this.key = key;
}
public String getMessage(String key){
return message + " from SecureMessage class";
}
public String getSourceName(){
return sourceName + " from SecureMessage class";
}
public String getDestName(){
return destName + " from SecureMessage class";
}
}
import java.util.ArrayList;
import java.util.Scanner;
public class Message {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String message = "blank", sourceName = "blank", destName = "blank", key = "blank", messageInput, userMessageDetails;
ArrayList<String> messageInputList = new ArrayList<>();
System.out.println("Enter message in form TO FROM MESSAGE : KEY \nExample --- 1 2 hey there how are you? : bubble123");
userMessageDetails = keyboard.nextLine();
String str = userMessageDetails;
String[] tokens = str.split(" ");
for(String s : tokens){
messageInputList.add(s);
}
System.out.println(messageInputList);
System.out.println("");
int x = messageInputList.size();
destName = messageInputList.get(0);
sourceName = messageInputList.get(1);
key = messageInputList.get(x-1);
//--this is where I am lost, because I need to pass a string to the SecureMessage getMessage() method---
// message = ???;
// I found out how to print the items, however this is not a string...
// for(int i = 2; i <= (x-3); i++){
// System.out.print(messageInputList.get(i) + " ");
// }
//------------------------------------------------------------------------------------------------------
SecureMessage secMsg = new SecureMessage(message, sourceName, destName, key);
System.out.println(secMsg.getDestName());
System.out.println(secMsg.getSourceName());
System.out.println(secMsg.getMessage(key));
}
}
答案1
得分: 2
这应该可以:
message = String.join(" ", messageInputList.subList(2, x-2));
英文:
This should do it:
message = String.join(" ", messageInputList.subList(2, x-2));
答案2
得分: 1
根据我所看到的,由于你是通过" "进行分割,那么你的消息将在messageInputList.get(2)和messageInputList.get(x-3)之间进行分割。简单的方法是使用循环并连接字符串。类似于:
String s_message = ""
for(int i = 2; i <= (x-3); i++){
s_message = s_message + messageInputList.get(i) + " "
}
英文:
So for what I see, as you split by " ", then you will have your message split between messageInputList.get(2) and messageInputList.get(x-3).
The easy way is having a for and concatenate strings. Something like:
String s_message = "";
for(int i = 2; i <= (x-3); i++){
s_message = s_message + messageInputList.get(i) + " ";
}
答案3
得分: 1
这将收集从索引2到x-2的messageInputList
中的元素,并将它们收集到一个名为message
的字符串中,您可以将其传递给您的SecureMessage
类。
英文:
message = IntStream.range(2, x - 2)
.mapToObj(messageInputList::get)
.collect(Collectors.joining(" "));
This will collect the elements from your messageInputList
from index 2 to x-2 and collect them to a singular String message
which you can pass to your SecureMessage
class.
答案4
得分: 0
我认为可以更简单一些:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("输入消息数据:");
System.out.println("-------------------");
System.out.print("目标名称:");
String destName = scan.next();
System.out.print("源名称:");
String sourceName = scan.next();
System.out.print("密钥:");
String key = scan.next();
System.out.println("消息(多行,双击回车结束):");
scan.useDelimiter("\n\n\n");
String message = scan.next().trim();
SecureMessage secMsg = new SecureMessage(message, sourceName, destName, key);
System.out.println(secMsg.getDestName());
System.out.println(secMsg.getSourceName());
System.out.println(secMsg.getMessage(key));
}
英文:
I think it could be much more simplier:
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter message data.");
System.out.println("-------------------");
System.out.print("Destination name: ");
String destName = scan.next();
System.out.print("Source name: ");
String sourceName = scan.next();
System.out.print("Key: ");
String key = scan.next();
System.out.println("Message (multi-line, double ENTER to finish):");
scan.useDelimiter("\n\n\n");
String message = scan.next().trim();
SecureMessage secMsg = new SecureMessage(message, sourceName, destName, key);
System.out.println(secMsg.getDestName());
System.out.println(secMsg.getSourceName());
System.out.println(secMsg.getMessage(key));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论