I want to change the letters A to point 1 and so the letter Z to be number 26, then changed again to number 27 letters AA, AB to 28. How do I? Do I have to use the "switch"? I use java program.
-
2First you have to define "change". Then you have to tell us what you've tried and what doesn't work. Is this supposed to work for any length string? – Falmarri Nov 24 '10 at 01:01
-
Can u help me how to put snapshot java in here? – Leostrada Nov 24 '10 at 01:25
-
You may want to consider: http://stackoverflow.com/questions/763691/programming-riddle-how-might-you-translate-an-excel-column-name-to-a-number as this operation is the reverse. Actually, voted to close -- this is exactly half of the problem solved. – Nov 24 '10 at 01:31
9 Answers
Did not test this, but something along these lines should work:
public String numberToCharacterRepresentation(int number) {
char[] ls = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
String r = "";
while(true) {
r = ls[number % 26] + r;
if(number < 26) {
break;
}
number /= 26;
}
return r;
}
The reverse:
public int stringToNumber(String str) {
char[] ls = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
Map<Character, Integer> m = new HashMap<Character, Integer>();
int j = 0;
for(char c: ls) {
m.put(c, j++);
}
int i = 0;
int mul = 1;
for(char c: new StringBuffer(str).reverse().toString().toCharArray()) {
i += m.get(c) * mul;
mul *= ls.length;
}
return i;
}

- 22,940
- 6
- 79
- 92

- 27,952
- 4
- 66
- 85
-
Wow thanks :D . But how to convert a next letter example AA = 27, AB = 28, AC = 29, AD = 30 ? – Leostrada Nov 24 '10 at 01:10
-
-
1@Jonathon Thanks for the nice edits! @Leostrada stringToNumber should give you the reverse. You should read about number base conversions, as this is basically a base-26 to base-10 conversion. Here's a sample link: http://www.cut-the-knot.org/recurrence/conversion.shtml – icyrock.com Nov 24 '10 at 01:34
How about using c-'A'+1 to convert the letter in c to the number you want? Calculating the next place would be the same except add 27 instead. Basically what you're doing is converting a base-26 number to decimal except you have no zero.

- 6,120
- 2
- 26
- 31
A simple solution is to treat the problem like writing letters instead of digits.
public static String asLetters(long num) {
StringBuilder sb = new StringBuilder();
while(num > 0) {
sb.append((char) ('@' + num % 26));
num /= 26;
}
return sb.toString();
}

- 525,659
- 79
- 751
- 1,130
For those of you wanting to do this for Excel:
public String getEquivColumn(int number){
String converted = "";
// Repeatedly divide the number by 26 and convert the
// remainder into the appropriate letter.
while (number >= 0)
{
int remainder = number % 26;
converted = (char)(remainder + 'A') + converted;
number = (number / 26) - 1;
}
return converted;
}

- 29,961
- 26
- 97
- 150
This will work for A to ZZ :
public static int columnCharToNumber(String str) {
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
if(str.length() == 1) {
return alphabet.indexOf(str);
}
if(str.length() == 2) {
return ( alphabet.indexOf(str.substring(1)) + 26*(1+alphabet.indexOf(str.substring(0,1)))) ;
}
return -1;
}

- 1
-
Hi Arif, welcome to SO and thank you for your answer. To make your answer even more useful, would you mind editing it (edit link just below the answer) to add in an explanation of _why_ this code solves the problem? It'll help people learn from and apply the solution to other situations, rather than just encourage copy-paste coding. Thanks again! – Tim Malone Sep 26 '16 at 00:30
This will do the job.
public String map(int i) {
String res = "";
if (i <= 0) {
throw new IllegalArgumentException("Can only map +ve numbers");
}
while (i > 0) {
res = Character.toString('A' + ((i - 1) % 26)) + res;
i = i / 26;
}
return res;
}
A more complicated version using a StringBuilder
would be a more efficient, but this one is easier to understand.

- 698,415
- 94
- 811
- 1,216
-
-
-
@Leostrada - assuming that's what you are asking, then the answer is to indent every line of code by a minimum of 4 space characters, and not use TAB characters. – Stephen C Nov 24 '10 at 03:35
Perhaps the simplest way for A-Z would be something like:
char c = *whatever letter you need*;
int cAsInt = Integer.toString(c - '@'); // @ is 1 less than A
For things like AA, BB, etc., it would depend on how many combinations you need. Setting up a mapping might be quickest, but if the possibilities are endless, you'll have to figure out some formula.

- 18,244
- 7
- 53
- 79
import javax.swing.JOptionPane;
public class TBesar{
public static long x(int a, int b){
if (b==0){
return(1);
}
else{
return(a*(x(a,(b-1))));
}
}
public static long KatakeAngka(String nama){
int A = 0;
int B = 26;
long C = 0;
long Z;
int panjang = nama.length();
char namas[] = new char[panjang];
for (int i=0;i<panjang;i++){
namas[i] = nama.charAt(i);
switch (namas[i]){
case 'a' : A=1;break;
case 'b' : A=2;break;
case 'c' : A=3;break;
case 'd' : A=4;break;
case 'e' : A=5;break;
case 'f' : A=6;break;
case 'g' : A=7;break;
case 'h' : A=8;break;
case 'i' : A=9;break;
case 'j' : A=10;break;
case 'k' : A=11;break;
case 'l' : A=12;break;
case 'm' : A=13;break;
case 'n' : A=14;break;
case 'o' : A=15;break;
case 'p' : A=16;break;
case 'q' : A=17;break;
case 'r' : A=18;break;
case 's' : A=19;break;
case 't' : A=20;break;
case 'u' : A=21;break;
case 'v' : A=22;break;
case 'x' : A=23;break;
case 'w' : A=24;break;
case 'y' : A=25;break;
case 'z' : A=26;break;
}
int D = panjang-(i+1);
Z = (x(B,D))*A;
C = C+Z;
}return(C);
}
public static String hitung(long angka){
String B ;
if(angka<27){
if(angka==1){
B="a";
}else if(angka==2){
B="b";
}else if(angka==3){
B="c";
}else if(angka==4){
B="d";
}else if(angka==5){
B="e";
}else if(angka==6){
B="f";
}else if(angka==7){
B="g";
}else if(angka==8){
B="h";
}else if(angka==9){
B="i";
}else if(angka==10){
B="j";
}else if(angka==11){
B="k";
}else if(angka==12){
B="l";
}else if(angka==13){
B="m";
}else if(angka==14){
B="n";
}else if(angka==15){
B="o";
}else if(angka==16){
B="p";
}else if(angka==17){
B="q";
}else if(angka==18){
B="r";
}else if(angka==19){
B="s";
}else if(angka==20){
B="t";
}else if(angka==21){
B="u";
}else if(angka==22){
B="v";
}else if(angka==23){
B="w";
}else if(angka==24){
B="x";
}else if(angka==25){
B="y";
}else{B="z";}
return(B);
}
else{
return(hitung(angka/26)+hitung(angka%26));
}
}
public static void main (String [] args){
String kata = JOptionPane.showInputDialog(null,"Masukkan Kata ke 1");
String kata2 = JOptionPane.showInputDialog(null, "Masukkan Kata ke 2");
long hasil = KatakeAngka(kata);
long hasil2 = KatakeAngka(kata2);
long total = hasil+hasil2;
String HasilKata = hitung(total);
JOptionPane.showMessageDialog(null,kata+" = "+hasil+"\n"+kata2+" = "+hasil2+"\n"+kata+" + "+kata2+" = "+HasilKata);
}
}