Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Increment a character...

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Here's a relatively simple question:
I'm writing a test class for a HashMap. I would like to generate sequential keys "A", "B", "C" etc...
Although the String and Character have a wide variety of methods, I have not found a good way to do this. Suggestions?
(If anyone was wondering, the way to do this in VB is s = chr(asc(s)+1). Thanks for helping with my retooling efforts.)
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I beleive you can just increment a char type variable using:
char letters;
letters++;
char is kind of a weird data type, you can increment it like an int, but when you use it, it will find the char value for the int, using Unicode values (I believe).
Hope this helps!
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matthew is correct. You can increment a char like a normal int. So,
char c = 'A';
c++;
will work.
Don't forget, if you want to use the char as a key in a hashmap, you need to convert it to an object first! Here is a bit of code to do so:-
--
HashMap h = new HashMap();
char c = 'A';
Character co = Character(c);
h.put(co, "Hello There!");
--
David.
 
Get off me! Here, read this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic