[Q#2899301][A#2899335] How do I increment a variable to the next or previous letter in the alphabet?
https://stackoverflow.com/q/2899301
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
https://stackoverflow.com/a/2899335
One way:
APIzation
String value = "C";
int charValue = value.charAt(0);
String next = String.valueOf( (char) (charValue + 1));
System.out.println(next);
package com.stackoverflow.api;
public class Human2899335 {
public static char incrementValueToChar(char value, int increment) {
char next = (char) (value + increment);
System.out.println(next);
return next;
}
}
package com.stackoverflow.api;
/**
* How do I increment a variable to the next or previous letter in the alphabet?
*
* @author APIzator
* @see <a href="https://stackoverflow.com/a/2899335">https://stackoverflow.com/a/2899335</a>
*/
public class APIzator2899335 {
public static String incrementVariable(String value) throws Exception {
int charValue = value.charAt(0);
String next = String.valueOf((char) (charValue + 1));
return next;
}
}