Results 1 to 11 of 11
  1. #1
    vinnie is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default Convert object to String

    I can't convert an element of an Object array to a String. (the object data is a string). When I use toString() it returns "Ljava.lang.Object;@10b30a7" the part after the @ differs based on the string. Any help will be appreciated. Thanks

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I can't convert an element of an Object array to a String. (the object data is a string). When I use toString() it returns "Ljava.lang.Object;@10b30a7"

    Could you post the code for this?

  3. #3
    vinnie is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    package receiver;

    import com.illposed.osc.*;
    import java.net.SocketException;
    /**
    *
    * @author Vinnie
    */
    public class Main {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) throws SocketException {
    OSCPortIn receiver = new OSCPortIn(12345);
    OSCListener listener = new OSCListener() {
    public void acceptMessage(java.util.Date time, OSCMessage message) {

    System.out.println(message.getArguments()[0]);
    //here is the code to print the string
    System.out.println(message.getArguments()[0].toString());
    }
    };
    receiver.addListener("/sayhello", listener);
    receiver.startListening();
    System.out.println("started");
    }

    }

  4. #4
    vinnie is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    its a program to communicate over a network with open sound control

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The key class here is the OSCMessage. It obviously doesn't have a toString override, and so you're seeing the default result from the Object class's toString call. The solution: either override the toString method of the OSCMessage class, or call another method of this class (perhaps it has a getMessage() method?) to extract the text you wish to see.

  6. #6
    vinnie is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    If I were to override toString(), what would I put inside?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by vinnie View Post
    If I were to override toString(), what would I put inside?
    Simply stated, you would put it inside of the code for class where you want to override this method. Which object is currently giving you a problem? Then that object's class is the one to modify. But the issue comes to fore if you can't modify this class, if it doesn't belong to you. If that's the case, then do my second suggestion.

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    It's not clear from that code that the elements of the array are strings as you said.

    In fact, getArguments() is returning an array whose first element, itself, an array of Objects.

    [Edit] Very slow posting this while I checked what an OSCMessage is. The array elements can be "a Float, String, Integer, BigInteger, Boolean or array of these".
    Last edited by pbrockway2; 12-12-2010 at 05:03 AM.

  9. #9
    vinnie is offline Member
    Join Date
    Dec 2010
    Posts
    5
    Rep Power
    0

    Default

    I realize that the object can be an int, float, bool etc. But in my sending app, I am using the first arg as a string all the time.

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I don't know anything about your sending app (you didn't post that). I do know that you printed using

    Java Code:
    System.out.println(message.getArguments()[0].toString());

    and that you got the output "Ljava.lang.Object;@10b30a7". That output is not random: it is saying, definitively, that the first element of the array returned by getArguments() is itself an array of Object references. The first element of the array returned by getArguments() is not a string.

    You might want to take that array - message.getArguments()[0] - and print out its elements to see what they are.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Just a guess, but maybe getArguments() is returning an array all of whose elements are arrays of the valid types. With some of them being one element arrays.

    In that case try

    Java Code:
    public class Main {
        public static void main(String[] args) throws SocketException {
            OSCPortIn receiver = new OSCPortIn(12345);
            OSCListener listener = new OSCListener() {
                public void acceptMessage(java.util.Date time, OSCMessage message) {
                    Object[] arg = (Object[])(message.getArguments()[0]);
                    System.out.println(arg[0]);
                }
            };
            receiver.addListener("/sayhello", listener);
            receiver.startListening();
            System.out.println("started");
        }
    }

Similar Threads

  1. Convert object to String
    By innspiron in forum New To Java
    Replies: 3
    Last Post: 03-14-2010, 01:26 PM
  2. How to convert string array into object in java
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 02-12-2010, 12:33 PM
  3. How to convert a string array into object in java
    By kgkamaraj in forum Advanced Java
    Replies: 2
    Last Post: 02-12-2010, 09:12 AM
  4. convert a string to an object in java
    By jforce93 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 11:57 PM
  5. Convert Comparable object to string or char
    By ScKaSx in forum New To Java
    Replies: 4
    Last Post: 01-25-2009, 02:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •