3

Im trying to crate a program that does this: 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 AA AB AC AD....ZZ AAA AAB AAC

The way I approached this is hard to put into words but I'll try explaining

I tried creating a base 27 system and make A represent 1, B->2 C->3, and AA->28 The problem is that every 27 letters where I get an @ representing 0.

I also tried making A represent 0 and having a base 26 system but 27 would be BA when I need it to be AA

public class aaa 
{
    public static void main(String args[]) 
{
    int counter=29;
    for(int x=0;x<=counter;x++)
    {   
        int quotient, remainder;
        String result="";
        quotient=x;

        while (quotient>0)
        {   
            remainder=quotient%27;
            result = (char)(remainder+64) + result;
            quotient = (int)Math.floor(quotient/27);

        }
    System.out.print(result+ " ");
           }
    }
}

This prints out 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 A@ AA AB

I want the program to do this 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 AA AB AC

0

5 Answers 5

2

There are 26 letters from A to Z. Your system is base 26, not 27.

What you'll probably want to do is:

  • Start x at 1, not 0. Your system is currently representing 0 as an empty string, which may be throwing you off.

  • Take the quotient and remainder modulo 26, not 27.

  • Add 65 (the ASCII value for 'A') to the remainder, not 64 (the ASCII value for '@').

1
  • Java uses Unicode, not ASCII. 65 is the one (for some characters, two are required) UTF-16 code unit value for 'A', a 16-bit number. Sep 11, 2014 at 23:00
1

There are 26 possible letters, so when you use the % and / operators, use 26 as the divisor.

A has to represent 1, or else the sequence would be equivalent to:

0 1 2 ... 25 00 01 02...

However, we would need the range of calculations to be in the range of 0-25.

With modifications to your code, I start with x as 1.

int counter=29;
for(int x=1;x<=counter;x++)
{

Later on in the while loop, I changed it to a do-while loop, and I subtract 1 from the quotient each time, to shift the domain from 1-26 to 0-25. Also, I add 65 ('A') to the remainder, so that 0 is mapped to 'A'.

    do
    {
        remainder=(quotient - 1)%26;
        result = (char)(remainder+65) + result;
        quotient = (int)Math.floor((quotient - 1)/26);
    }
    while (quotient>0);

Output:

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 AA AB AC
1
  • Thanks! It worked perfectly. I should've thought of that haha. Sep 11, 2014 at 21:40
0

As a start, as several answers already mentioned, there are 26 letters, so use a base 26 system, not 27.

Besides that, print A as 0, instead of @, so change (char)(remainder + 64) to (char)(remainder + 65). The last thing you need to do is change quotient = (int)Math.floor(quotient/27); because you want to print A, that will be 0 in this scale, so subtract 1 from it and stop loop when quotient smaller than 0.

public class HelloWorld{

     public static void main(String []args){
        int counter=59;
        for(int x=0; x<=counter; x++)
        {   
            int quotient, remainder;
            String result="";
            quotient=x;

            while (quotient >= 0)
            {
                remainder = quotient % 26;
                result = (char)(remainder + 65) + result;
                quotient = (int)Math.floor(quotient/26) - 1;
            }
            System.out.print(result+ " ");
        }
     }
}

Output (note that there is no space at start of output also):

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 AA AB AC AD AE AF AG AH AI AJ AK AL AM AN AO AP AQ AR AS AT AU AV AW AX AY AZ BA BB BC BD BE BF BG BH

Ps: Indent your code correctly!

0
0

From this question and its answers I think there's a simpler approach: Convert the number to base 27, and then replace each character with the appropriate letter (as pointed by Pshemo's comment, you need to define a 'zero' character... I'll assume that _ will stand for zero). A HashMap will hold the custom characters that you want to use to print the number.

import java.util.HashMap;

public class BaseConverter
{
    /**
     * Converts an integer to base 27, and returns the string with the custom characters
     * defined in the map.
     */
    public static String convertToMyBase27(int a) {
        HashMap<Character, Character> m = new HashMap<>();
        m.put('0', '_'); // Or another Zero character
        m.put('1', 'A');
        m.put('2', 'B');
        m.put('3', 'C');
        m.put('4', 'D');
        m.put('5', 'E');
        m.put('6', 'F');
        m.put('7', 'G');
        m.put('8', 'H');
        m.put('9', 'I');
        m.put('a', 'J');
        m.put('b', 'K');
        m.put('c', 'L');
        m.put('d', 'M');
        m.put('e', 'N');
        m.put('f', 'O');
        m.put('g', 'P');
        m.put('h', 'Q');
        m.put('i', 'R');
        m.put('j', 'S');
        m.put('k', 'T');
        m.put('l', 'U');
        m.put('m', 'V');
        m.put('n', 'W');
        m.put('o', 'X');
        m.put('p', 'Y');
        m.put('q', 'Z');

        String ans = "";
        String s = Integer.toString(a, 27);
        for(char c : s.toLowerCase().toCharArray()) {
            ans += m.get(c);
        }
        return ans;
    }
}
0

Create a char array containing you letter like char letters[] = new char[]{'A','B', ... }. write a simple loop like do ... while(num > 0). Mod the number with letters.length and add the resulting char to a StringBuilder. Devide the number by letters.length. When the loop finishes revers the output and you are done.

Update:

public class MagicNumbers
{
    private static final char[] LETTERS = { '@', '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 final int LLEN = LETTERS.length;

    private static String toMagicString(final int num)
    {
        int temp = num < 0 ? -num : num;

        StringBuilder strB = new StringBuilder();

        do
        {
            strB.append(LETTERS[temp % LLEN]);
            temp /= LLEN;
        }
        while (temp > 0);

        if (num < 0)
        {
            strB.append('-');
        }

        return strB.reverse().toString();
    }


    public static void main(final String[] args)
    {

        for (int i = -28; i < 29; i++)
        {
            System.out.println(toMagicString(i));
        }

    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.