Jump to content
  • Advertisement
Sign in to follow this  
Snash

[java] Convert CPlayer to string

This topic is 5030 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
Advertisement
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
Yep.

toString() is for I/O of some (at least vaguely) human-readable representation of an object, typically for debugging.

Object*Streams are for serialization, which is what you seem to be talking about.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!