Question

In my JADE program, one agent needs to send an ACL message to another agent. For the agent sending the message (agent1) it stores a String[] array of values that it has to send.

However, in order to actually send the ACL message the content must only be a String and nothing else. The method used add content to the message is the following : msg.setContent(String str)

So the problem is I have a range of values stored in agent1 , which are all in an array. I have to send these values in ONE message so I can't send several messages with each element of the array. In my current "Test" array I only put two elements so this is what I'm doing so far:

msg.setContent(theArray[0] + theArray[1]);

Now when the receiving agent (agent2) opens this message and gets the content it's obviously just a concatenation of the two elements of the array I sent from agent1.

How do I get agent2 to split this one String back into an array of String[] ? I have looked at the method

split(String regex)

for the String value of the message content. So I'm thinking since each element of the array in Agent1 starts with a Capital letter, then maybe I could enter a regular expression to split String as soon as a capital letter is encountered.

However I'm not sure how to do this, or if it's even a good idea. Please provide any suggestions.

Relevant API doc:

http://jade.cselt.it/doc/api/jade/lang/acl/ACLMessage.html#setContent(java.lang.String)

Was it helpful?

Solution 2

You can use JSON as an interchange format in order to send pretty much anything as String over the wire.

Here is an example using org.json.

Collection c = Arrays.asList(str);
org.json.JSonArray arr = new org.json.JSonArray(c);
msg.sendContents(arr.toString());

On the other side:

String s = getContents();
org.json.JSonArray arr = new org.json.JSonArray(s);
String[] strs = new String[arr.length()];
for (int i = 0; i < arr.length(); i++) {
    strs[i] = arr.getString(i);
}

OTHER TIPS

You can use java.util.Arrays class to convert an array into a string

Like :

String [] myArray = new String[3];
array[0] = "Abc";
array[1] = "Def";
array[2] = "Xyz";
String s =java.util.Arrays.toString(myArray);

So now s will have a string [Abc, Def, Xyz]

Now for converting back from string to string array,

all you have to do is remove those [ and ] first(get the substring) and then split the string.

String myString = s.substring(1, s.length()-1);
String arrayFromString[] = myString.split(", ");

Refer this link java.util.Arrays javadoc

Note: This will not work if your strings contain , (comma and a single space) as mentioned by @jlordo

The solution in Abu's answer will work just fine IF your strings will never ever contain ", ".

If they do, you need to chose something else as the separator (a newline \n for instance). If you cannot be sure about any character never ever appearing in the text, then I guess it cannot be done.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top