Search code examples
javastringagents-jade

Convert from String[] to String and again to String[]


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)


Solution

  • 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);
    }