• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Byte to String problem

 
Johannes Thorén
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now i realized that I'm comparing strings by using == operator. But even when i changed it to .equalsIgnoreCase() it doesint work :/
 
John Jai
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Seems that both conditions call the run() method.... What you get when you try to print the msg.toString()
 
Johannes Thorén
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Johannes Thorén
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Harsha Smith
Ranch Hand
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
y don't use equalsIgnoreCase or contains method instead of compareToIgnoreCase?
 
Johannes Thorén
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 287
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try removing the white spaces as told?

 
Johannes Thorén
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could not trim a byte array, but i tried to make a string and then trim the string.
 
Johannes Thorén
Ranch Hand
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic