This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Increment a character... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Increment a character..." Watch "Increment a character..." New topic

Increment a character...

Michael Mendelson
Ranch Hand

Joined: Dec 19, 2000
Posts: 73

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.)
Matthew Jones
Ranch Hand

Joined: Dec 21, 2000
Posts: 68
I beleive you can just increment a char type variable using:
char 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!
David Harrigan
Ranch Hand

Joined: Dec 12, 2000
Posts: 52
Matthew is correct. You can increment a char like a normal int. So,
char c = 'A';
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!");
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link:
subject: Increment a character...
jQuery in Action, 3rd edition