Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Please check out this : Roman to decimal

807598Oct 23 2006 — edited Oct 23 2006
Well Hi friends.....

I don't know how much appreciation goes into for programmers with good programing skills........

but i would like you all to please criticize and help me out a better algorithm for this program which converts roman to decimal and vice versa.....

MIND YOU....THIS Program is working perfectly..........

also please note the following : decimal to roman

1 - I, 5 - V, 10 - X, 50 - L, 100 - C, 500 - D, 1000 - M


Please COPY Paste the follwing code and run it

the program will take input as:

1st - number of numbers...
then the numbers either in decimal or roman form and convert them appropriately
/**
 * RomanDecimal.java
 *
 * Created on October 12, 2006, 12:50 PM
 *
 */
package vaibhav_exp1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author Pingle's Home
 */
public class RomanDecimal {
    
    private String romans = "IVXLCDM";
    private int[] decimals = {1, 5, 10, 50, 100, 500, 1000};
    public String[] numbers;
    public int numOfEntries;
    public static int sameOccurence = 1;
    public static boolean valid = true;
    
    /** Creates a new instance of RomanDecimal */
    public RomanDecimal() {
    }
    
    public void setNumOfEntries(int noe) {
        numOfEntries = noe;
    }
    
    public void getData() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter Number of Entries : ");
        setNumOfEntries(Integer.parseInt(br.readLine()));
        if(numOfEntries !=0) {
            numbers = new String[numOfEntries];
            for(int i = 0; i<numOfEntries; i++) {
                System.out.println("Number " + (i+1) + "> ");
                String s = br.readLine();
                numbers[i] = s;
            }
        }
        br.close();
    }
        
    public int romanToDecimal(String roman) {
        int i = 0,decimal = 0;
        int len = roman.length();
        int temp = 0;
        
        if(i < len) {
            char cc = roman.charAt(i);
            switch(cc) {
                case 'I':
                    if((i+1) < len) {
                        char nc = roman.charAt(i+1);
                        if(nc == 'V' || nc == 'X') {
                            decimal = decimals[romans.indexOf(nc)] - decimals[romans.indexOf(cc)];
                            i++;
                            sameOccurence = 1;
                        } else if(nc == 'I') {
                            sameOccurence += 1;
                            if(sameOccurence > 3) {
                                sameOccurence = 1;
                                valid = false;
                                System.out.println("Invalid Roman Representation 1");
                                return 0;
                            }
                            decimal = decimals[romans.indexOf(cc)];
                        } else {
                            sameOccurence = 1;
                            valid = false;
                            System.out.println("Invalid Roman Representation 2");
                            return 0;
                        }
                    } else {
                        decimal = decimals[romans.indexOf(cc)];
                    }
                    break;
                case 'X':
                    if((i+1) < len) {
                        char nc = roman.charAt(i+1);
                        if(nc == 'X' || nc == 'I' || nc == 'V') {
                            if(nc == 'X') {
                                sameOccurence += 1;
                                if(sameOccurence > 3) {
                                    sameOccurence = 1;
                                    valid = false;
                                    System.out.println("Invalid Roman Representation 1");
                                    return 0;
                                }
                            }
                            if(nc == 'I') sameOccurence = 1;
                            decimal = decimals[romans.indexOf(cc)];
                        } else if(nc == 'L' || nc == 'C') {
                            decimal = decimals[romans.indexOf(nc)] - decimals[romans.indexOf(cc)];
                            i++;
                            sameOccurence = 1;
                        } else {
                            sameOccurence = 1;
                            valid = false;
                            System.out.println("Invalid Roman Representation 2");
                            return 0;
                        }
                    } else {
                        decimal = decimals[romans.indexOf(cc)];
                    }
                    break;
                case 'C':
                    if((i+1) < len) {
                        char nc = roman.charAt(i+1);
                        if(nc == 'C' || nc == 'I' || nc == 'X' || nc == 'V' || nc == 'L') {
                            if(nc == 'C') {
                                sameOccurence += 1;
                                if(sameOccurence > 3) {
                                    sameOccurence = 1;
                                    valid = false;
                                    System.out.println("Invalid Roman Representation 1");
                                    return 0;
                                }
                            }
                            if(nc == 'I' || nc == 'X' || nc == 'V' || nc == 'L') sameOccurence = 1;
                            decimal = decimals[romans.indexOf(cc)];
                        } else if(nc == 'D' || nc == 'M') {
                            decimal = decimals[romans.indexOf(nc)] - decimals[romans.indexOf(cc)];
                            i++;
                            sameOccurence = 1;
                        } else {
                            sameOccurence = 1;
                            valid = false;
                            System.out.println("Invalid Roman Representation 2");
                            return 0;
                        }
                    } else {
                        decimal = decimals[romans.indexOf(cc)];
                    }
                    break;
                case 'V':
                    if((i+1) < len) {
                        char nc = roman.charAt(i+1);
                        if(nc == 'I') {
                            sameOccurence = 1;
                            decimal = decimals[romans.indexOf(cc)];
                        } else {
                            valid = false;
                            System.out.println("Invalid Roman Representation 2");
                            return 0;
                        }
                    } else {
                        decimal = decimals[romans.indexOf(cc)];
                    }
                    break;
                case 'L':
                    if((i+1) < len) {
                        char nc = roman.charAt(i+1);
                        if(nc == 'I' || nc == 'X' || nc == 'V') {
                            sameOccurence = 1;
                            decimal = decimals[romans.indexOf(cc)];
                        } else {
                            valid = false;
                            System.out.println("Invalid Roman Representation 2");
                            return 0;
                        }
                    } else {
                        decimal = decimals[romans.indexOf(cc)];
                    }
                    break;
                case 'D':
                    if((i+1) < len) {
                        char nc = roman.charAt(i+1);
                        if(nc == 'I' || nc == 'X' || nc == 'V' || nc == 'L' || nc == 'C') {
                            sameOccurence = 1;
                            decimal = decimals[romans.indexOf(cc)];
                        } else {
                            valid = false;
                            System.out.println("Invalid Roman Representation 2");
                            return 0;
                        }
                    } else {
                        decimal = decimals[romans.indexOf(cc)];
                    }
                    break;
                case 'M':
                    if((i+1) < len) {
                        char nc = roman.charAt(i+1);
                        if(nc == 'M') {
                            sameOccurence += 1;
                            if(sameOccurence > 3) {
                                sameOccurence = 1;
                                valid = false;
                                System.out.println("Invalid Roman Representation 2");
                                return 0;
                            }
                        }
                    }
                    decimal = decimals[romans.indexOf(cc)];
                    break;
                default:
            }
        }
        if((i+1) < len)
            temp = romanToDecimal(roman.substring(i+1));
        return decimal + temp;
    }
    
    public String decimalToRoman(int decimal) {
        String roman = "";
        int quo = decimal;
        int rem = 0;
        int len = String.valueOf(decimal).length();
        String [][] rtod = {{"", "", "", ""},
        {"I", "X", "C", "M"},
        {"II", "XX", "CC", "MM"},
        {"III", "XXX", "CCC", "MMM"},
        {"IV", "XL", "CD", ""},
        {"V", "L", "D", ""},
        {"VI", "LX", "DC", ""},
        {"VII", "LXX", "DCC", ""},
        {"VIII", "LXXX", "DCCC", ""},
        {"IX", "XC", "CM", ""}};
        
        if(decimal > 3999) {
            System.out.println("Out of Roman Representation Range...!!!");
        } else {
            for(int i = 0; i<len; i++) {
                int factor = (int)Math.pow((double)10, (double)i);
                rem = quo%10;
                quo = quo/10;
                roman = rtod[rem] + roman;
}
}
return roman;
}

public void convertData() {
System.out.println("\n\nThe Results are as follows : ");
for(int i = 0; i<numOfEntries; i++) {
String currNum = numbers[i].toUpperCase();
sameOccurence = 1;
valid = true;
try {
String answer = decimalToRoman(Integer.parseInt(currNum));
System.out.println("Roman of " + currNum + " > "+ answer);
} catch (NumberFormatException ex) {
int answer = romanToDecimal(numbers[i]);
System.out.println("Decimal of " + currNum + " > "+ answer);
}
}
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
RomanDecimal rd = new RomanDecimal();
try {
rd.getData();
} catch (IOException ex) {
ex.printStackTrace();
}
rd.convertData();
}
}

Thank You,

Vaibhav


Message was edited by:
vaibhavpingle
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 20 2006
Added on Oct 23 2006
46 comments
760 views