I am trying to create a method that will find the next column after a given column. For example:
input: A
output: B
It seems quite simple at first. I was just going to use the following method:
public static char nextLetter(char c) {
c++;
return c;
}
The problem arises when you get past column Z. In Google Sheets, after column Z, column names are two letters, then three, etc. So after column Z
comes AA
, after AZ
comes BA
, after ZZ
comes AAA
, etc. My next thought was to first figure out the column position in terms of index. So column AA
would 27, BA
52, etc.
Finding the index of the column is not the problem I'm facing right now. I need to figure out how to convert that index to the corresponding column name. I was going to try the following method, but I realized that it is also limited to A-Z:
public static char getLetter(int index) {
return (char) (index + 64);
}
At this point, I am thinking that a recursive method is needed. However, I cannot figure out to set it up. This is as far as I got:
private static void getNotation(int size) {
int divided = size / 26;
int remainder = size % 26;
String notation = "";
while (divided % 26 > 0) {
// Here is where the need for a recursive method comes in
}
}
Does anyone know a good way to convert an integer (index) to the corresponding column name?
EDIT
I just found a very helpful resource on Github which deals with hexavigesimals: https://gist.github.com/pinguet62/9817978