How do I increment a variable to the next or previous letter in the alphabet?

raleighJ picture raleighJ · May 24, 2010 · Viewed 57.4k times · Source

I have a capital letter defined in a variable string, and I want to output the next and previous letters in the alphabet. For example, if the variable was equal to 'C', I would want to output 'B' and 'D'.

Answer

Kevin picture Kevin · May 24, 2010

One way:

String value = "C";
int charValue = value.charAt(0);
String next = String.valueOf( (char) (charValue + 1));
System.out.println(next);