Results 1 to 11 of 11
Thread: Convert object to String
- 12-12-2010, 04:11 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
- 12-12-2010, 04:22 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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?
- 12-12-2010, 04:34 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
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");
}
}
- 12-12-2010, 04:35 AM #4
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
its a program to communicate over a network with open sound control
-
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.
- 12-12-2010, 04:54 AM #6
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
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.
- 12-12-2010, 05:01 AM #8
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 12-12-2010, 05:09 AM #9
Member
- Join Date
- Dec 2010
- Posts
- 5
- Rep Power
- 0
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.
- 12-12-2010, 05:26 AM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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.
- 12-12-2010, 05:45 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
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
-
Convert object to String
By innspiron in forum New To JavaReplies: 3Last Post: 03-14-2010, 01:26 PM -
How to convert string array into object in java
By kgkamaraj in forum New To JavaReplies: 4Last Post: 02-12-2010, 12:33 PM -
How to convert a string array into object in java
By kgkamaraj in forum Advanced JavaReplies: 2Last Post: 02-12-2010, 09:12 AM -
convert a string to an object in java
By jforce93 in forum Advanced JavaReplies: 1Last Post: 08-09-2009, 11:57 PM -
Convert Comparable object to string or char
By ScKaSx in forum New To JavaReplies: 4Last Post: 01-25-2009, 02:02 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks