help converting Roman to decimal in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • capoeira26
    New Member
    • Sep 2009
    • 2

    help converting Roman to decimal in java

    I have is that my entered Roman numerals work only if the numerlas are entered in large to smaller format. For exapmple if I type MMC program will correctly display 2100, but when I enter MCM it will display 2100 instead of 1900. I know that I need to come up with a way to determine what letter comes before so program can subtract it but at this point am little stuck. Here is my code:




    package chapt08;


    import javax.swing.JOp tionPane;

    /* Write a program that converts a number entered in Roman numerals to decimal. Your program should consists

    * of a class, say Roman. An objects of the types Roman should do the following:

    * a) store the number as Roman numeral

    * b) Convert and store the number into decimal

    * c) print the number as a Roman numeral or decimal number as requested by the user

    *

    * The decimal values of the Roman numerals are

    * M 1000

    * D 500

    * C 100

    * L 50

    * X 10

    * V 5

    * I 1

    *

    * d) test your program using the following Roman numerals: MCXIV, CCCLIX and MDCLXVI

    *

    */


    Code:
    public class Roman
    {
    
    static String romanNumeral;
    static int decimalNum;
    static char convertToDecimal;
    
    public static void main(String args[])
    {
      Roman demo = new Roman();
      demo.convertToDecimal(decimalNum);
      printRoman(romanNumeral);
      printDecimal(decimalNum);
      setRoman();
      toString(romanNumeral);
    } 
    
    public void convertToDecimal (int other)
    {
      romanNumeral = JOptionPane.showInputDialog("Enter a Roman numeral to convert to decimal." + "\n\n"+ "Note: Roman numerals are I, V, X, L, C, D and M."+ "\n" + "All letters entered will be treated as capitalized.");
      romanNumeral = romanNumeral.toUpperCase();
      int x = 0;
      do
      {
        convertToDecimal = romanNumeral.charAt(x); //return a char value from the first character in the string
        switch(convertToDecimal)
        {
          case 'M':
            decimalNum += 1000;
            break;
          case 'D':
            decimalNum += 500;
            break;
          case 'C':
            decimalNum += 100;
            break;
          case 'L':
            decimalNum += 50;
            break;
          case 'X':
            decimalNum += 10;
            break;
          case 'V':
            decimalNum += 5;
            break;
          case 'I':
            decimalNum += 1;
            break;
        }
        x++;
      }while(x<romanNumeral.length()); 
    
      //make a condition here...
        JOptionPane.showMessageDialog(null, "Roman Numeral Converted to Decimal is: " + decimalNum);// + ch, 
      
        //other=decimalNum.getCopy;
      }
    
      public static void printRoman (String romanNumeral){
        System.out.println ("Roman Numeral stored is: " + romanNumeral);
      }
    
      public static void printDecimal (int decimalNum){
        System.out.println ("Decimal Number stored is: " + decimalNum); 
      }
    
      public static void setRoman (){
        System.out.println("test Set Roman");
      }
    
      // public boolean equals (String romanNumeral, int decimalNum ){
      // return (romanNumeral == decimalNum);
      // }
    
      public void Other(String temp)
      {
        temp = "";
      }
    
      public Roman getCopy(int decimalNum)
      {
        Roman temp = new Roman();
        Roman.decimalNum = decimalNum;
        System.out.println(" temp is: " + temp);
        return temp;
      }
    
    
      public static String toString(String str)
      {
        //str = "test";
        if (str==romanNumeral)
          System.out.println("Roman numeral " + romanNumeral + " is equal to Decimal Number " + decimalNum);
      else
          System.out.println("Roman numeral " + romanNumeral + " is not equal to Decimal Number " + str);
        return str;
      }
    }
    Last edited by Frinavale; Oct 2 '09, 08:45 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    The basic rule states that Roman numerals are read from left to right, larger valued letters precede smaller valued letters, and multiple letters in a row represent multiples of that value. For instance:

    I = 1
    II = 2
    III = 3
    IV = 4
    V = 5
    VI = 6
    VII = 7
    VIII = 8
    CVIII = 108
    CCVIII = 208
    DCCVIII = 708
    MDCCVIII = 1708

    If a larger value letter is preceded by a single smaller value letter, then the combination represents the value of the larger value letter minus the value of the smaller value letter. This exception only holds true for the values 4, 9, 40, 90, 400, and 900, which are written as IV, IX, XL, XC, CD, and CM respectively.

    So you need to write cases for when this exception holds true in order to properly calculate the Roman numeral value.
    Last edited by Frinavale; Jan 25 '12, 02:18 PM.

    Comment

    • Ivahnaj
      New Member
      • Jan 2012
      • 1

      #3
      try this function i made ...simplest possible... so far wherever i tried it worked... if there's any mistake please be sure to tell me...
      Code:
      int RomanTodecimal(String roman)
          {
              int l= roman.length();
              int i;
              int deci=0;
              int num=0;
              for (i=l-1;i>=0;i--)
              { char x = roman.charAt(i);
                  x = Character.toUpperCase(x);
                  switch(x)
                  {  case 'I':
                      num = 1;
                      break;
                     case 'V':
                      num = 5;
                      break;
                      case 'X':
                      num = 10;
                      break;
                      case 'L':
                      num = 50;
                      break;
                      case 'C':
                      num = 100;
                      break;
                      case 'D':
                      num = 500;
                      break;
                      case 'M':
                      num = 1000;
                      break;
                      default:
                      { System.out.print ("Error");
                        System.exit(0); 
                      }
                  }
                  if (num<deci)
                  {deci= deci-num;}
                   else
                  deci= deci+num;
              }
               return deci;
              }
      Last edited by Frinavale; Jan 16 '12, 02:13 PM. Reason: Added code tags.

      Comment

      • yash1234
        New Member
        • Feb 2012
        • 2

        #4
        @Ivahnaj:

        This program is returning 1510 instead of 1910 for MDCCCCX

        Here's the simple solution:

        Code:
        import java.util.*;
        
        public class RomanToDecimal {
                public static void main(String args[]) {
                        int decimal = 0;
                        Scanner input = new Scanner(System.in);
                        System.out.print("Enter a Roman Number: ");
                        String roman = input.next();
                        String romanNumeral = roman.toUpperCase();
                        int x = 0;
                        do {
                                char convertToDecimal = roman.charAt(x);
                                switch (convertToDecimal) {
                                case 'M':
                                        decimal += 1000;
                                        break;
        
                                case 'D':
                                        decimal += 500;
                                        break;
        
                                case 'C':
                                        decimal += 100;
                                        break;
        
                                case 'L':
                                        decimal += 50;
                                        break;
        
                                case 'X':
                                        decimal += 10;
                                        break;
        
                                case 'V':
                                        decimal += 5;
                                        break;
        
                                case 'I':
                                        decimal += 1;
                                        break;
                                }
                                x++;
                        } while (x < romanNumeral.length());
                        System.out.println("Decimal Number is: " + decimal);
                }
        }
        Last edited by Niheel; Feb 29 '12, 08:32 AM.

        Comment

        Working...