Convert letter to digits
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? D开发者_StackOverflowo I have to use the "switch"? I use java program.
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;
}
Use the Character object 0=>0 a=>10, etc If you only use letters then subtract 10
Character.forDigit(10,Character.MAX_RADIX) //will return 'a'
Character.getNumericValue('a') // will return 10
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();
}
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;
}
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.
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.
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.
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);
}
}
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;
}
精彩评论