Skip to main content
gamedev.net
🔒 Locked

[java] Convert CPlayer to string

Started by Snash Sep 12, 2004 at 1:56 PM 2 replies 1k views
Original Post
Snash
Snash
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...
luxitan
luxitan
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
To be considered a genius you just have to say what everybody knows in a way very few understand
Zahlman
Zahlman
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.