• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Why my String keys can't be found if I create them with Arrays.toString() method?

 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I don't understand why my Map can't find the key I put into it. If I print the Map, I can see the key is present. Here is the code.



Following is the output.

run:
Current Size =1
[abc0]
Current Size =2
[abc1]
[abc0]
Current Size =3
[abc1]
[abc0]
[abc2]
Current Size =4
[abc1]
[abc0]
[abc3]
[abc2]
Current Size =5
[abc4]
[abc1]
[abc0]
[abc3]
[abc2]
Current Size =6
[abc4]
[abc1]
[abc0]
[abc5]
[abc3]
[abc2]
Current Size =7
Before size =7
After size =7
[abc4]
Before size =7
After size =7
[abc6]
Current Size =3
[abc4]
[abc7]
[abc6]
Current Size =4
[abc4]
[abc8]
[abc7]
[abc6]
Current Size =5
[abc4]
[abc9]
[abc8]
[abc7]
[abc6]
null
[[abc4], [abc9], [abc8], [abc7], [abc6]]


The problem is with this part of the output,

null
[[abc4], [abc9], [abc8], [abc7], [abc6]]

corresponding to following lines.
System.out.println(map.get("abc9".intern()));
System.out.println(map.keySet());

I am guessing it has to do something with the way I am creating my String objects in the following method. But I don't know why is it not working correctly.



Earlier I had coded it as follows.


I had the same problem. I thought may be I need to create a new String. So I changed



to



I still had the same problem.

Then I changed the method as follows.



This worked. I was able to retrieve the value for the keys that were not reclaimed. But I can't understand why only return new String("abc" + i); works but the other way doesn't work.

Please help.

Thanks,
Chan.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Array.toString() puts brackets around the output, so char[] ['a']['b']['c']['9'] produces "[abc9]" as an output, not "abc9".

To turn the char[] to the String you want, you probably want to do new String(a)
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What are those pesky "[" and "]" characters overall?

(Note: I was too lsow....)
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Steve.

*FacePalm* :-)

Although I was not aware that Arrays.toString() puts those brackets around the output, I couldn't even spot the error from the output that was repeating the case over and again.
Not that I hadn't tried enough to spot the error myself. Thanks.

Chan.


 
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting one...I am curious:

What was the point of using Arrays.toString()? It seems like it needlessly complicates things in other ways, not just by adding the brackets.

Also, why do you have the line to strip commas, when you know for certain that there will never be a comma in the temporary string?


 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What was the point of using Arrays.toString()? It seems like it needlessly complicates things in other ways, not just by adding the brackets.

Also, why do you have the line to strip commas, when you know for certain that there will never be a comma in the temporary string?



Actually my requirement was to put random Strings into the map. So initially my CreateTemporaryStrings method was like this.



But this resulted in ASCII codes of all kinds that I couldn't understand. I didnt and don't know how to restrict the method to return alphanumeric random Strings. I thought I'd find out about that later.
I thought there'd be something else the API would have. So I started creating a char array and then realized there was no nextChars method. Because I had already created a char array by then, I thought I'd test a few char toString and String.toChar and Arrays.toString cause I had forgotten about them since I'd read K&B's SCJP book. I know it was not right to code like that, but it was just rough work. But while experimenting this, I got stuck cause I couldn't find those keys in the map because of the brackets. I couldn't spot the error too.

Chan.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote:But this resulted in ASCII codes of all kinds that I couldn't understand.


And that's because they aren't ASCII codes. Furthermore, not even all regular ASCII codes (0-127) are printable.

So, maybe you need to look at your requirements a bit closer.

Also: Java bytes are signed.

Winston
 
Scott Shipp
Ranch Hand
Posts: 239
12
Scala IntelliJ IDE Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Chan Ag wrote:

What was the point of using Arrays.toString()? It seems like it needlessly complicates things in other ways, not just by adding the brackets.

Also, why do you have the line to strip commas, when you know for certain that there will never be a comma in the temporary string?



Actually my requirement was to put random Strings into the map. So initially my CreateTemporaryStrings method was like this.



But this resulted in ASCII codes of all kinds that I couldn't understand. I didnt and don't know how to restrict the method to return alphanumeric random Strings. I thought I'd find out about that later.
I thought there'd be something else the API would have. So I started creating a char array and then realized there was no nextChars method. Because I had already created a char array by then, I thought I'd test a few char toString and String.toChar and Arrays.toString cause I had forgotten about them since I'd read K&B's SCJP book. I know it was not right to code like that, but it was just rough work. But while experimenting this, I got stuck cause I couldn't find those keys in the map because of the brackets. I couldn't spot the error too.

Chan.



Ah, that makes more sense.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

And that's because they aren't ASCII codes. Furthermore, not even all regular ASCII codes (0-127) are printable.



Do you mean they are simply bit sequences?

Do you mean some bit sequences ( of the range -128 to 127 ) do not have a printable ASCII representation or do you mean bytes are represented/printed in some other encoding?

Thanks,
Chan.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I get it now. You must mean all of them don't have a printable ASCII conversion character. Fine, I get it. :-)

Thanks,
Chan.
 
Ivan Jozsef Balazs
Rancher
Posts: 1044
6
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Furthermore, not even all regular ASCII codes (0-127) are printable.



Those codes include terminal/device and message control characters like "start of heading", "enquiry", "acknowledge", "data link escape" etc.

ASCII

ASCII reserves the first 32 codes (numbers 0–31 decimal) for control characters: codes originally intended not to represent printable information, but rather to control devices (such as printers) that make use of ASCII,
or to provide meta-information about data streams such as those stored on magnetic tape. For example, character 10 represents the "line feed" function
(which causes a printer to advance its paper), and character 8 represents "backspace".
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Ivan.
 
If you're gonna buy things, buy this thing and I get a fat kickback:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic