Sign in to follow this  

[java] Convert CPlayer to string

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi.. I've just started writing a small multiplayer game.. Right now i'm having a problem converting my CPLayer data to a simple string. public class CPlayer { int score; int x; int y; } CPLayer Player; I want to be able to "convert" player to a string, containing the bytes. eg: String string=Player; and then the string would hold the "raw" data of the player. But how do i do this? The reason is I want to send the string using: out.println(string); recieve the string, and convert it to CPlayer again. but how ? Thanks for the help...

Share this post


Link to post
Share on other sites
this goes in CPlayer class:


public String toString()
{
return x + ";" + y + ";" + score;
}



To read it back you could use a StringTokenizer with ";" for token.


StringTokenizer st = new StringTokenizer(the_string, ";");

int x = Integer.parseInt(st.nextToken());
int y = Integer.parseInt(st.nextToken());
int score = Integer.parseInt(st.nextToken());



this was made without testing so could have errors

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Don't use a String to send the data. You can use DataInputStream and DataOutputStream to read and write the data. You can also use serialsization with ObjectInputStream and ObjectOutputStream to automaticly read and write the object.

Share this post


Link to post
Share on other sites

This topic is 4835 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this