aspose file tools*
The moose likes Beginning Java and the fly likes Byte to String problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Byte to String problem" Watch "Byte to String problem" New topic
Author

Byte to String problem

Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
I'm trying to make a simple "Hangman-game". I've got 3 classes that's interesting in this case.

My problem is that, I'm sending a message to my server, containing of a string "start_game". In my connectionhandler I've said that "If the message is start_game, run the method run() in "HangmanGame". The problem is that it doesint call the method "run()" but instead calls "guess()". (it should only call guess if the string sent is only 1 char long).

The second problem is that it's saying "Some of the data got lost..." since i specified that the message returned from the server should be of the same size as the message sent. The problem is, I wont know beforehand how big the return message will be.

Here are the classes:

SimpleClient.java



SimpleConnectionHandler (hg is a HangmanGame object)


HangmanGame



Thanks in forehand

Kind Regards,
Drace
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
And yeah the size on fromServer is way to big, but i get the same when i try it smaller.
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
Now i realized that I'm comparing strings by using == operator. But even when i changed it to .equalsIgnoreCase() it doesint work :/
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776

Seems that both conditions call the run() method.... What you get when you try to print the msg.toString()
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
I call the run method in both, but thats not the problem. I cant print the msg.toString() since its on the server side. But when i print the byte by sending it back out.write(msg) i get "start_game". But the comparing does not work. It doesint return 0 even tho their equal.

"start_game".compareTo(msg.toString())
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3534
    
  15
Johannes Thorén wrote:I call the run method in both, but thats not the problem. I cant print the msg.toString() since its on the server side. But when i print the byte by sending it back out.write(msg) i get "start_game". But the comparing does not work. It doesint return 0 even tho their equal.

"start_game".compareTo(msg.toString())

If msg does contain start_game then that should work. Are you sure there are no spaces at the beginning or end ? Try posting your latest code - the problem may be elsewhere


Joanne
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
msg is an array of bytes containing "start_game". So msg.toString() should contain a string with the content "start_game" right? Here's the new code:




And the client that sends the message to the server side is:




Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287

Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Johannes Thorén wrote:msg is an array of bytes containing "start_game". So msg.toString() should contain a string with the content "start_game" right?



wrong. Look at the above piece of code
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
Harsha Smith wrote:
Johannes Thorén wrote:msg is an array of bytes containing "start_game". So msg.toString() should contain a string with the content "start_game" right?



wrong. Look at the above piece of code


But that's on the client side.

1. I make a string "start_game"
2. convert it to bytes to send to the server side.
3. recieve the bytes on server side
4. convert the bytes to a string
5. compare the new string with the string "start_game".
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
y don't use equalsIgnoreCase or contains method instead of compareToIgnoreCase?
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
Harsha Smith wrote:y don't use equalsIgnoreCase or contains method instead of compareToIgnoreCase?


+100k


Thank you, did not see that one

Altho it does not work. But now i realize that i only take 4096 bytes out of the message from the client. But that should be enough for "start_game" right?
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
Maybe the fact that the byte array is 4096 bytes long makes the equalsIgnoreCase false? Even if it contains start_game it still contains empty byte-slots aswell?
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
Did you try removing the white spaces as told?

Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
I could not trim a byte array, but i tried to make a string and then trim the string.
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
I think it works now tho. But i dont get any result from my game-class hehe. But thats another problem
Johannes Thorén
Ranch Hand

Joined: Nov 18, 2008
Posts: 64
This is the new problem. When i call "run()" i append information to the StringBuilder sb. And when i fetch the sb using method getAnswer(), it is empty.
Here's the parts of the game-glass:

>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Byte to String problem