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

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

Is there a way to make a string from ArrayList elements?

问题

我想要求用户输入一条消息:

> "按以下格式输入消息:"TO FROM MESSAGE : KEY""

用户输入示例:

> 1 2 嗨,你好吗?: bubble123

然后,我想让SecureMessage类返回这条消息。然而,我不知道如何创建消息部分的字符串,因为getMessage()方法需要一个分配给它的字符串。

SecureMessage类不应该有任何修改器。SecureMessage中的所有字段都从Message类中设置。

  1. public class SecureMessage {
  2. private String message;
  3. private String sourceName;
  4. private String destName;
  5. private String key;
  6. public SecureMessage(String message, String sourceName, String destName, String key){
  7. this.message = message;
  8. this.sourceName = sourceName;
  9. this.destName = destName;
  10. this.key = key;
  11. }
  12. public String getMessage(String key){
  13. return message + " 来自SecureMessage类";
  14. }
  15. public String getSourceName(){
  16. return sourceName + " 来自SecureMessage类";
  17. }
  18. public String getDestName(){
  19. return destName + " 来自SecureMessage类";
  20. }
  21. }
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. public class Message {
  4. public static void main(String[] args) {
  5. Scanner keyboard = new Scanner(System.in);
  6. String message = "空白", sourceName = "空白", destName = "空白", key = "空白", messageInput, userMessageDetails;
  7. ArrayList<String> messageInputList = new ArrayList<>();
  8. System.out.println("按照格式 TO FROM MESSAGE : KEY 输入消息\n示例 --- 1 2 嗨,你好吗?: bubble123");
  9. userMessageDetails = keyboard.nextLine();
  10. String str = userMessageDetails;
  11. String[] tokens = str.split(" ");
  12. for(String s : tokens){
  13. messageInputList.add(s);
  14. }
  15. System.out.println(messageInputList);
  16. System.out.println("");
  17. int x = messageInputList.size();
  18. destName = messageInputList.get(0);
  19. sourceName = messageInputList.get(1);
  20. key = messageInputList.get(x-1);
  21. //--这是我迷失的地方,因为我需要向SecureMessage的getMessage()方法传递一个字符串---
  22. // message = ???;
  23. // 我找到了如何打印这些项,但这不是一个字符串...
  24. // for(int i = 2; i <= (x-3); i++){
  25. // System.out.print(messageInputList.get(i) + " ");
  26. // }
  27. //------------------------------------------------------------------------------------------------------
  28. SecureMessage secMsg = new SecureMessage(message, sourceName, destName, key);
  29. System.out.println(secMsg.getDestName());
  30. System.out.println(secMsg.getSourceName());
  31. System.out.println(secMsg.getMessage(key));
  32. }
  33. }
英文:

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.

  1. public class SecureMessage {
  2. private String message;
  3. private String sourceName;
  4. private String destName;
  5. private String key;
  6. public SecureMessage(String message, String sourceName, String destName, String key){
  7. this.message = message;
  8. this.sourceName = sourceName;
  9. this.destName = destName;
  10. this.key = key;
  11. }
  12. public String getMessage(String key){
  13. return message + &quot; from SecureMessage class&quot;;
  14. }
  15. public String getSourceName(){
  16. return sourceName + &quot; from SecureMessage class&quot;;
  17. }
  18. public String getDestName(){
  19. return destName + &quot; from SecureMessage class&quot;;
  20. }
  21. }
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3. public class Message {
  4. public static void main(String[] args) {
  5. Scanner keyboard = new Scanner(System.in);
  6. String message = &quot;blank&quot;, sourceName = &quot;blank&quot;, destName = &quot;blank&quot;, key = &quot;blank&quot;, messageInput, userMessageDetails;
  7. ArrayList&lt;String&gt; messageInputList = new ArrayList&lt;&gt;();
  8. System.out.println(&quot;Enter message in form TO FROM MESSAGE : KEY \nExample --- 1 2 hey there how are you? : bubble123&quot;);
  9. userMessageDetails = keyboard.nextLine();
  10. String str = userMessageDetails;
  11. String[] tokens = str.split(&quot; &quot;);
  12. for(String s : tokens){
  13. messageInputList.add(s);
  14. }
  15. System.out.println(messageInputList);
  16. System.out.println(&quot;&quot;);
  17. int x = messageInputList.size();
  18. destName = messageInputList.get(0);
  19. sourceName = messageInputList.get(1);
  20. key = messageInputList.get(x-1);
  21. //--this is where I am lost, because I need to pass a string to the SecureMessage getMessage() method---
  22. // message = ???;
  23. // I found out how to print the items, however this is not a string...
  24. // for(int i = 2; i &lt;= (x-3); i++){
  25. // System.out.print(messageInputList.get(i) + &quot; &quot;);
  26. // }
  27. //------------------------------------------------------------------------------------------------------
  28. SecureMessage secMsg = new SecureMessage(message, sourceName, destName, key);
  29. System.out.println(secMsg.getDestName());
  30. System.out.println(secMsg.getSourceName());
  31. System.out.println(secMsg.getMessage(key));
  32. }
  33. }

答案1

得分: 2

这应该可以:

  1. message = String.join(" ", messageInputList.subList(2, x-2));
英文:

This should do it:

  1. message = String.join(&quot; &quot;, messageInputList.subList(2, x-2));

答案2

得分: 1

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

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

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:

  1. String s_message = &quot;&quot;;
  2. for(int i = 2; i &lt;= (x-3); i++){
  3. s_message = s_message + messageInputList.get(i) + &quot; &quot;;
  4. }

答案3

得分: 1

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

英文:
  1. message = IntStream.range(2, x - 2)
  2. .mapToObj(messageInputList::get)
  3. .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

我认为可以更简单一些:

  1. public static void main(String[] args) {
  2. Scanner scan = new Scanner(System.in);
  3. System.out.println("输入消息数据:");
  4. System.out.println("-------------------");
  5. System.out.print("目标名称:");
  6. String destName = scan.next();
  7. System.out.print("源名称:");
  8. String sourceName = scan.next();
  9. System.out.print("密钥:");
  10. String key = scan.next();
  11. System.out.println("消息(多行,双击回车结束):");
  12. scan.useDelimiter("\n\n\n");
  13. String message = scan.next().trim();
  14. SecureMessage secMsg = new SecureMessage(message, sourceName, destName, key);
  15. System.out.println(secMsg.getDestName());
  16. System.out.println(secMsg.getSourceName());
  17. System.out.println(secMsg.getMessage(key));
  18. }
英文:

I think it could be much more simplier:

  1. public static void main(String[] args) {
  2. Scanner scan = new Scanner(System.in);
  3. System.out.println(&quot;Enter message data.&quot;);
  4. System.out.println(&quot;-------------------&quot;);
  5. System.out.print(&quot;Destination name: &quot;);
  6. String destName = scan.next();
  7. System.out.print(&quot;Source name: &quot;);
  8. String sourceName = scan.next();
  9. System.out.print(&quot;Key: &quot;);
  10. String key = scan.next();
  11. System.out.println(&quot;Message (multi-line, double ENTER to finish):&quot;);
  12. scan.useDelimiter(&quot;\n\n\n&quot;);
  13. String message = scan.next().trim();
  14. SecureMessage secMsg = new SecureMessage(message, sourceName, destName, key);
  15. System.out.println(secMsg.getDestName());
  16. System.out.println(secMsg.getSourceName());
  17. System.out.println(secMsg.getMessage(key));
  18. }

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:

确定