• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Problem with Returning string

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!I would like to answer something about strings.I have made a hashmap which the key is a char,especially a letter and the value is two integers.Each letter corresponds in two integers.For example h thas the key 99.I want to print each phrase i give as a stirng of integers.Especially for hello i want o print it like 9987656534. To return this string of integers i have done this:  String.valueOf(a1.values()); a1 is my hashmap but my result is [99,87,65,65,34].How can fix this?
Thank you!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post the actual code ?
 
Marshal
Posts: 80874
506
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch JJ.

You cannot express 9987656534 as an int/Integer; it is outwith its range. If you are getting [99,87,65,65,34], that must be printing a List<Integer>. Please explain what you are doing, as JJ asked. Is this some sort of encoding and decoding exercise?
 
Rancher
Posts: 5127
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 [99,87,65,65,34].How can fix this?


If that is a String, the String class has several methods that will allow you to create a new String without the [], characters.
 
Bartender
Posts: 242
27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are probably plenty of helper methods in Apache Commons to do this for you, but it's actually a good exercise to write this kind of thing from scratch once.

This is what I would do, step by step:
- Create an empty StringBuilder
- Retrieve the KeySet from the Map
- Loop through the KeySet (for each key)
- Retrieve the value for that key
- Append the value onto the StringBuilder
- After the loop, return the StringBuilder.toString()
 
Saloon Keeper
Posts: 5657
214
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What puzzles me is the following: the outcome, a Collection of Integers, is the result of String.valueOf(a1.values()).

In this is a1 the HashMap that OP talks about in his opening post. Given the result, the keys must have been h, e, l and o (no duplicates). So, how come an output of five integers? Wonder what the code looks like, as Joao asked.
 
Saloon Keeper
Posts: 11127
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the OP meant "two integers" to mean "an integer with two digits".
 
Piet Souris
Saloon Keeper
Posts: 5657
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think so too, but I wonder what the keys were, given 5 values with 2 duplicates.
 
reply
    Bookmark Topic Watch Topic
  • New Topic