JAVA PROGRAM TO CONVERT ROMAN TO NUMERIC
public class RomantoDec {
public static void main(String[] args){
int M=1000,D=500,C=100,L=50,X=10,V=5,I=1;
int decimal=0;
String roman = args[0];
String romanNumeral = roman.toUpperCase();
String comRom[] ={"IV","IX","XL","XC","CD","CM"};
String genRom[] = new String[romanNumeral.length()-1];
for(int i=0,j=1;i<romanNumeral.length()&&j<romanNumeral.length();i++,j++){
genRom[i]= Character.toString(romanNumeral.charAt(i)) + Character.toString(romanNumeral.charAt(j));
}
for(int k=0;k<genRom.length;k++){
//System.out.print(genRom[k]+"\t");
String romo=genRom[k];
if(romo.equals("IV")){
decimal += V-I;
decimal = decimal-I;
decimal = decimal-V;
}
if(romo.equals("IX")){
decimal += X-I;
decimal = decimal-I;
decimal = decimal-X;
}
if(romo.equals("XL")){
decimal += L-X;
decimal = decimal-X;
decimal = decimal-L;
}
if(romo.equals("XC")){
decimal += C-X;
decimal = decimal-X;
decimal = decimal-C;
}
if(romo.equals("CD")){
decimal += D-C;
decimal = decimal-C;
decimal = decimal-D;
}
if(romo.equals("CM")){
decimal += M-C;
decimal = decimal-C;
decimal = decimal-M;
}
//}
}
int x = 0;
do {
char convertToDecimal = romanNumeral.charAt(x);
switch (convertToDecimal) {
case 'M':
decimal += M;
break;
case 'D':
decimal += D;
break;
case 'C':
decimal += C;
break;
case 'L':
decimal += L;
break;
case 'X':
decimal += X;
break;
case 'V':
decimal += V;
break;
case 'I':
decimal += I;
break;
}
x++;
} while (x < romanNumeral.length());
System.out.println("Decimal Number is: " + decimal);
}
}
No comments:
Post a Comment