• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

LinkedList Problem?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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").

 
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anayonkar Shivalkar wrote:
Hint 2: You are printing 'value' only once - after coming out of while loop


 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic