method/function to generate the column names for a spreadsheet application similar to Excel

I'm trying to achieve the below output, e.g if i pass 5 to the function columnNames, it should print A, B, C, D, E and if pass 27 it should print A, B, C, D, E...AA, BB, CC etc.
I need to work with the below code snippet and I only need to work on the columnNames method.
  import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.math.*;
    import java.util.regex.*;

public class Tester {

    static List<String> columnNames(int n) {
        List<String> result = new ArrayList<String>();


        return result;
    }

    public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);

        int _columns = Integer.parseInt(in.nextLine().trim());

        List<String> result = columnNames(_columns);

        System.out.println(String.join(", ", result));
    }
}
when i added this snippet to the columnNames method and passed 5 to the parameter, it only prints the column letter equivalent to the number i entered. However, I expected to see A,B,C,D,E.
StringBuilder sb = new StringBuilder();
    while (n > 0) {

    for (int i = 0; i < n; i++) {
              n--;
              char ch = (char) (n % 26 + 'A');
              n /= 26;
              sb.append(ch);
              result.add(sb.toString());
         }

     }
    sb.reverse();
Thanks for the help.

Комментарии

Популярные сообщения из этого блога

Skipping acquire of configured file 'contrib/binary-i386/Packages' as repository … doesn't support architecture 'i386'

Как преобразовать вертикальную запись в горизонтальную?

Connection string for MariaDB using ODBC