• Post Reply Bookmark Topic Watch Topic
  • New Topic

LinkedList Problem?  RSS feed

 
Waldemar Macijewski
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Everyone, I have this strange LinkedList problem, not sure if the problem is within the list itself, or maybe the code, as I'm not very familiar with data structures yet. Before I get started, I understand that some of you might be wondering why I need to make all these data converting, from String to byte[] array, then from byte array to char, and eventually call Character.toString(). Well I will be sending some data over the network to servlet, the servlet will have to process this data and return to client. The data is stored in byte array, then I cycle through the array, convert every element into char (since you can cast byte to char), add every element into LinkedList, and extract what I need as a String by calling Character.toString(). Eventually what I need is, a few Strings send from the client. I understand, that there might be some better alternatives to this (and I'm sure there is), but still, why my code below doesn't work?.

What I'm trying to do here, is encode String parameter "name" into byte array, then cycle through the array and as I said earlier convert every element into char, then add these elements into the List. Then iterate over the list, and extract values that I need, by calling Character.toString(). So the output, is always the last character in the message, if type "abcdefgh" the output will be "h", if I type "Spain", the output is "n". So why is that?
I suspect that every node in my list just points to characters of the whole String. Even if it is that, why only "h" and not "a" ? (in case of "abcdefgh").

 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Okti,

It is still unclear to me what exactly are you doing, and why do you need to convert String into array of byte, and then convert each byte into char.

Anyways, did you try to debug the program?

Hint: When you print 'value', what is content of it?
Hint 2: You are printing 'value' only once - after coming out of while loop

I hope this helps.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I also don't really understand what your problem is, can you explain what are are actually seeing and what you expected to see.

and extract what I need as a String by calling Character.toString().


This will only convert a single char into a String ie you will have a String containing the single char, is this what you wanted?
 
Waldemar Macijewski
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:Hi Okti,
It is still unclear to me what exactly are you doing, and why do you need to convert String into array of byte, and then convert each byte into char.

Hey thanks for your time. What I'm trying to do, is convert String into byte array, and send this array to servlet. Now the servlet, will have to process this data, and find those Strings that I want. I'm note sure, how can you do that, but I thought since you can cast byte into char, I thought LinkedList<Character> would be appropriate data structure, and then using methods like "it.next()" extract what I need.

When I run my program, I see:

List size: 8
Value: h

I expected to see, Value: abcdefgh, and not just "h".

This will only convert a single char into a String ie you will have a String containing the single char, is this what you wanted?

Well no, I want all of the chars to be converted.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:
Hint 2: You are printing 'value' only once - after coming out of while loop

 
Angus Comber
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working this out for myself too.

As for conversion from Java String (which is unicode string) to byte (which is a single byte integer) you can do like this below.

To convert from String to byte array do this:



To convert the other way round you do this:




So to send a String over the network where a byte stream is required you can do this sort of thing:


For receiving an input byte stream and converting to a String you could iterate through the byte array and convert each byte to a char. Or:

 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm note sure, how can you do that, but I thought since you can cast byte into char, I thought LinkedList<Character> would be appropriate data structure, and then using methods like "it.next()" extract what I need.

You can re-create a String directly from an array of bytes using one the of String constructor's which takes an array of bytes.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!