Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert an integer to A1 notation?

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

like image 579
Cardinal System Avatar asked Dec 18 '19 23:12

Cardinal System


2 Answers

I created an example:

class Test {
    static char[] alphabet = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                               'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                               'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };

    private static String indexToColumnName(int i) {
        if (i >= alphabet.length) {
            return indexToColumnName((int)Math.floor(i / alphabet.length) - 1)
              + indexToColumnName(i % alphabet.length);
        }
        return Character.toString(alphabet[i]);
    }

    public static void main(String args[]) {
        for (int i = 0; i <= 800; ++i) {
            System.out.println(i + ": " + indexToColumnName(i));
        }
    }
}

The above will yield something like:

0: A
1: B
2: C
3: D
...
24: Y
25: Z
26: AA
27: AB
...
700: ZY
701: ZZ
702: AAA
703: AAB
...

In this case, it's zero-index, but that you could easily change yourself.

like image 167
kba Avatar answered Sep 28 '22 04:09

kba


You can do it as follows:

public class Main {
    public static void main(String[] args) {
        System.out.println(getNextColumn("A"));
        System.out.println(getNextColumn("Z"));
        System.out.println(getNextColumn("AA"));
        System.out.println(getNextColumn("AZ"));
        System.out.println(getNextColumn("ZA"));
        System.out.println(getNextColumn("ZZ"));
        System.out.println(getNextColumn("AAA"));
        System.out.println(getNextColumn("ABA"));
        System.out.println(getNextColumn("ABZ"));
        System.out.println(getNextColumn("ZZZ"));
    }

    static String getNextColumn(String column) {
        column = column.toUpperCase();
        StringBuilder sb = new StringBuilder();
        boolean allZ = true;
        for (int i = 0; i < column.length(); i++) {
            if (!(column.charAt(i) == 'Z')) {
                allZ = false;
                break;
            }
        }
        if (allZ) {
            for (int i = 0; i <= column.length(); i++) {
                sb.append('A');
            }
            return sb.toString();
        }
        boolean charAfterZ = false;
        int indexOfZ = 0;
        for (int i = 0; i < column.length() - 1; i++) {
            if (column.charAt(i) == 'Z') {
                charAfterZ = true;
                indexOfZ = i;
                break;
            }
        }
        if (charAfterZ) {
            sb.append(column.substring(0, indexOfZ + 1) + (char) (column.charAt(indexOfZ + 1) + 1));
            if (column.length() > indexOfZ + 2) {
                sb.append(column.substring(indexOfZ + 1));
            }
            return sb.toString();
        }

        char lastChar = column.charAt(column.length() - 1);

        if (lastChar == 'Z') {
            sb.append(column.substring(0, column.length() - 2) + (char) (column.charAt(column.length() - 2) + 1) + ""
                    + 'A');
        } else {
            if (column.length() > 1) {
                sb.append(column.substring(0, column.length() - 1) + ""
                        + (char) (column.charAt(column.length() - 1) + 1));
            } else {
                sb.append((char) (column.charAt(column.length() - 1) + 1));
            }
        }
        return sb.toString();
    }
}

Output:

B
AA
AB
BA
ZB
AAA
AAB
ABB
ACA
AAAA
like image 38
Arvind Kumar Avinash Avatar answered Sep 28 '22 04:09

Arvind Kumar Avinash