有没有办法从ArrayList元素中创建一个字符串?

huangapple go评论92阅读模式
英文:

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 + &quot; from SecureMessage class&quot;;
}
public String getSourceName(){
return sourceName + &quot; from SecureMessage class&quot;;
}
public String getDestName(){
return destName + &quot; from SecureMessage class&quot;;
}
}
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 = &quot;blank&quot;, sourceName = &quot;blank&quot;, destName = &quot;blank&quot;, key = &quot;blank&quot;, messageInput, userMessageDetails;
ArrayList&lt;String&gt; messageInputList = new ArrayList&lt;&gt;();
System.out.println(&quot;Enter message in form TO FROM MESSAGE : KEY \nExample --- 1 2 hey there how are you? : bubble123&quot;);
userMessageDetails = keyboard.nextLine();
String str = userMessageDetails;
String[] tokens = str.split(&quot; &quot;);        
for(String s : tokens){
messageInputList.add(s);
}
System.out.println(messageInputList);
System.out.println(&quot;&quot;);
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 &lt;= (x-3); i++){
//            System.out.print(messageInputList.get(i) + &quot; &quot;);
//        }
//------------------------------------------------------------------------------------------------------
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(&quot; &quot;, messageInputList.subList(2, x-2));

答案2

得分: 1

根据我所看到的,由于你是通过" "进行分割,那么你的消息将在messageInputList.get(2)和messageInputList.get(x-3)之间进行分割。简单的方法是使用循环并连接字符串。类似于:

String s_message = &quot;&quot;

for(int i = 2; i &lt;= (x-3); i++){
    s_message = s_message + messageInputList.get(i) + &quot; &quot;
}
英文:

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 = &quot;&quot;;
for(int i = 2; i &lt;= (x-3); i++){
s_message = s_message + messageInputList.get(i) + &quot; &quot;;
}

答案3

得分: 1

这将收集从索引2到x-2的messageInputList中的元素,并将它们收集到一个名为message的字符串中,您可以将其传递给您的SecureMessage类。

英文:
message = IntStream.range(2, x - 2)
.mapToObj(messageInputList::get)
.collect(Collectors.joining(&quot; &quot;));

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(&quot;Enter message data.&quot;);
System.out.println(&quot;-------------------&quot;);
System.out.print(&quot;Destination name: &quot;);
String destName = scan.next();
System.out.print(&quot;Source name: &quot;);
String sourceName = scan.next();
System.out.print(&quot;Key: &quot;);
String key = scan.next();
System.out.println(&quot;Message (multi-line, double ENTER to finish):&quot;);
scan.useDelimiter(&quot;\n\n\n&quot;);
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));
}

huangapple
  • 本文由 发表于 2020年10月1日 04:14:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/64145049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定