Incrementing charaters past 'Z' in Java like a Spreadsheet

Incrementing charaters past 'Z' in Java like a Spreadsheet

increment char c
increment char java
how to increment letters in java
increment char python
how to increment each character in a string using java
increment each character in the string by 1 python
increment char c#
ascii values

I didn't start too long ago with programming, and currently I need a method to produce an array, containing a character which is comes after the previous character. It should start with an 'A' at 0, then a B at '1' etc.. The hard part is making it so that after the 'Z' comes 'AA'.

What I came up with:

public static String[] charArray(int length)
{   
    String[] res = new String[length];
    for(int i = 0; i < length; i++)
    {
        String name = "";
        int colNumber = i;
        while(colNumber > 0)
        {
            char c = (char) ('A' + (colNumber % 26));
            name = c + name;
            colNumber = colNumber / 26;
        }
        res[i] = name;
    }
    return res;
}

This works fine for the first 26 letters of the alphabet, but it produces "... Y, Z, BA, BB, BC..." instead of "... Y, Z, AA, AB, AC..."

What's wrong? Or are there any more efficient or easier ways to do this?

Thanks in advance!


You had a nice start. Instead of running through the while loop this example basically calculates the value of C based on the number % 26

Then the letter is added (concatenated) to the value within the array at the position: (index / 26) - 1 which ensures it's keeping up with the changes over time.

When iterating through on the first go, you'll have only one letter in each slot in the array A B C etc.

Once you've run through the alphabet, you'll then have an index that looks backwards and adds the current letter to that value.

You'll eventually get into AAA AAB AAC etc. or even more.

    public static String[] colArray(int length) {   

    String[] result = new String[length];

    String colName = "";
    for(int i = 0; i < length; i++) {

        char c = (char)('A' + (i % 26));
        colName = c + "";
        if(i > 25){
            colName =  result[(i / 26) - 1] + "" + c;
        }
        result[i] = colName;
    }
    return result;
}

Ways to increment a character in Python, For e.g Adding 1 to a character, if we require to increment the character, (most recent call last): File "/home/fabc221bf999b96195c763bf3c03ddca.py", line 9,  Incrementing charaters past 'Z' in Java like a Spreadsheet. I didn't start too long ago with programming, and currently I need a method to produce an array, containing a character which is comes after the previous character. It should start with an 'A' at 0, then a B at '1' etc.. The hard part is making it so that after the 'Z' comes 'AA'.


Try like this:

public static String[] charArray(int length)
{   
    String[] res = new String[length];
    int counter = 0;
    for(int i = 0; counter < length; i++)
    {
        String name = "";
        int colNumber = i;
        while(colNumber > 0 && colNumber % 27 != 0)
        {  
            char c = (char) ('A' + ((colNumber) % 27) - 1);
            name = c + name;
            colNumber = colNumber / 27;
        }
        res[counter] = name;

        if (i % 27 != 0) {
            counter++;
        }
    }
    return res;
}

Basically your algorithm skipped all elements starting with an A (A, AA, AB, ...) (because an A is created when colNumber is 0, but this never happens because your while terminates in that case). Taking modulo of 27 and then actually subtracting 1 after from the char fixes this issue. Then we use counter as index as otherwise we would end up with some empty elements in the array (the ones where i would be i % 27 == 0).

Ways to increment a character in python, TypecastingLet's first see what happens if we add an int to char . recent call last​) <ipython-input-20-312932410ef9> in <module>() 3 4 ## try to add To increment a character in a Python, we have to convert it into an integer  The pre-increment and pre-decrement operators increment (or decrement) their operand by 1, and the value of the expression is the resulting incremented (or decremented) value. The post -increment and post -decrement operators increase (or decrease) the value of their operand by 1, but the value of the expression is the operand's original value prior to the increment (or decrement) operation.


This solution works for me. Having 26 vocabulary letters and knowing that 65 is the char 'A' in ASCII table, we can get the incrementing with this recursive method...

private fun indexLetters(index: Int, prefix: String = "") : String {
    val indexSuffix:Int = index.rem(26)
    val suffix = (65 + indexSuffix).toChar().toString().plus(".")
    val newPrefix = suffix.plus(prefix)

    val indexPrefix: Int = index / 26
    return if (indexPrefix > 0) {
        indexLetters(indexPrefix - 1, newPrefix)
    } else {
        newPrefix
    }
}

You can call this kotlin method like

indexLetters(0) //To get an 'A'
indexLetters(25) //To get a 'Z'
indexLetters(26) //To get an 'A.A'
etcetera...

from an array iteration, depending of your requirements

Incrementing Character Values » Amadeus, Incrementing numeric values can be easily achieved using SAS software, but Find the ASCII equivalent value for our last character value in our code using the​  This is a reference page for increment verb forms in present, past and participle tenses. Find conjugation of increment. Check past tense of increment here.


C Primer Plus, read a character while there is more input increment character Count if a line has You can identify this case by keeping track of the previous character read. increment (third-person singular simple present increments, present participle incrementing, simple past and past participle incremented) (intransitive, transitive) To increase by steps or by a step, especially by one. 1890, H. E. J. G. Du Bois, “On Magnetic Circuits”, in Philosophical magazine‎, page 346:


Standard C++ IOStreams and Locales: Advanced Programmer's Guide , The character referred to before the iterator increment cannot be obtained from a pointer to the last valid position is expected to yield the past-the-end position. Doctors is a British soap opera that began broadcasting on BBC One on 26 March 2000. The following is a list of characters who appear, or formerly appeared, listed in order of first appearance.


Software Engineering: Proceedings of the Third Symposium on , INFO is 0 if the last operation was one that extracted a character from the line buffer. Character operations advance the pointer circularly, incrementing it by l​  Incrementing a string is a little trickier than incrementing a number. You have to convert the character to ASCII, increment the value and then convert it back to a character. Here's a piece of code to increment a single character: DECLARE @chOneLetter char(1) Set @chOneLetter = 'A' SELECT @chOneLetter = CHAR(ASCII(@chOneLetter) + 1) This