469,282 Members | 1,701 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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.JOptionPane;

/* 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

*

*/


Expand|Select|Wrap|Line Numbers
  1. public class Roman
  2. {
  3.  
  4. static String romanNumeral;
  5. static int decimalNum;
  6. static char convertToDecimal;
  7.  
  8. public static void main(String args[])
  9. {
  10.   Roman demo = new Roman();
  11.   demo.convertToDecimal(decimalNum);
  12.   printRoman(romanNumeral);
  13.   printDecimal(decimalNum);
  14.   setRoman();
  15.   toString(romanNumeral);
  16.  
  17. public void convertToDecimal (int other)
  18. {
  19.   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.");
  20.   romanNumeral = romanNumeral.toUpperCase();
  21.   int x = 0;
  22.   do
  23.   {
  24.     convertToDecimal = romanNumeral.charAt(x); //return a char value from the first character in the string
  25.     switch(convertToDecimal)
  26.     {
  27.       case 'M':
  28.         decimalNum += 1000;
  29.         break;
  30.       case 'D':
  31.         decimalNum += 500;
  32.         break;
  33.       case 'C':
  34.         decimalNum += 100;
  35.         break;
  36.       case 'L':
  37.         decimalNum += 50;
  38.         break;
  39.       case 'X':
  40.         decimalNum += 10;
  41.         break;
  42.       case 'V':
  43.         decimalNum += 5;
  44.         break;
  45.       case 'I':
  46.         decimalNum += 1;
  47.         break;
  48.     }
  49.     x++;
  50.   }while(x<romanNumeral.length()); 
  51.  
  52.   //make a condition here...
  53.     JOptionPane.showMessageDialog(null, "Roman Numeral Converted to Decimal is: " + decimalNum);// + ch, 
  54.  
  55.     //other=decimalNum.getCopy;
  56.   }
  57.  
  58.   public static void printRoman (String romanNumeral){
  59.     System.out.println ("Roman Numeral stored is: " + romanNumeral);
  60.   }
  61.  
  62.   public static void printDecimal (int decimalNum){
  63.     System.out.println ("Decimal Number stored is: " + decimalNum); 
  64.   }
  65.  
  66.   public static void setRoman (){
  67.     System.out.println("test Set Roman");
  68.   }
  69.  
  70.   // public boolean equals (String romanNumeral, int decimalNum ){
  71.   // return (romanNumeral == decimalNum);
  72.   // }
  73.  
  74.   public void Other(String temp)
  75.   {
  76.     temp = "";
  77.   }
  78.  
  79.   public Roman getCopy(int decimalNum)
  80.   {
  81.     Roman temp = new Roman();
  82.     Roman.decimalNum = decimalNum;
  83.     System.out.println(" temp is: " + temp);
  84.     return temp;
  85.   }
  86.  
  87.  
  88.   public static String toString(String str)
  89.   {
  90.     //str = "test";
  91.     if (str==romanNumeral)
  92.       System.out.println("Roman numeral " + romanNumeral + " is equal to Decimal Number " + decimalNum);
  93.   else
  94.       System.out.println("Roman numeral " + romanNumeral + " is not equal to Decimal Number " + str);
  95.     return str;
  96.   }
  97. }
Sep 26 '09 #1
3 17825
Frinavale
9,735 Expert Mod 8TB
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.
Oct 2 '09 #2
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...
Expand|Select|Wrap|Line Numbers
  1. int RomanTodecimal(String roman)
  2.     {
  3.         int l= roman.length();
  4.         int i;
  5.         int deci=0;
  6.         int num=0;
  7.         for (i=l-1;i>=0;i--)
  8.         { char x = roman.charAt(i);
  9.             x = Character.toUpperCase(x);
  10.             switch(x)
  11.             {  case 'I':
  12.                 num = 1;
  13.                 break;
  14.                case 'V':
  15.                 num = 5;
  16.                 break;
  17.                 case 'X':
  18.                 num = 10;
  19.                 break;
  20.                 case 'L':
  21.                 num = 50;
  22.                 break;
  23.                 case 'C':
  24.                 num = 100;
  25.                 break;
  26.                 case 'D':
  27.                 num = 500;
  28.                 break;
  29.                 case 'M':
  30.                 num = 1000;
  31.                 break;
  32.                 default:
  33.                 { System.out.print ("Error");
  34.                   System.exit(0); 
  35.                 }
  36.             }
  37.             if (num<deci)
  38.             {deci= deci-num;}
  39.              else
  40.             deci= deci+num;
  41.         }
  42.          return deci;
  43.         }
Jan 15 '12 #3
@Ivahnaj:

This program is returning 1510 instead of 1910 for MDCCCCX

Here's the simple solution:

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class RomanToDecimal {
  4.         public static void main(String args[]) {
  5.                 int decimal = 0;
  6.                 Scanner input = new Scanner(System.in);
  7.                 System.out.print("Enter a Roman Number: ");
  8.                 String roman = input.next();
  9.                 String romanNumeral = roman.toUpperCase();
  10.                 int x = 0;
  11.                 do {
  12.                         char convertToDecimal = roman.charAt(x);
  13.                         switch (convertToDecimal) {
  14.                         case 'M':
  15.                                 decimal += 1000;
  16.                                 break;
  17.  
  18.                         case 'D':
  19.                                 decimal += 500;
  20.                                 break;
  21.  
  22.                         case 'C':
  23.                                 decimal += 100;
  24.                                 break;
  25.  
  26.                         case 'L':
  27.                                 decimal += 50;
  28.                                 break;
  29.  
  30.                         case 'X':
  31.                                 decimal += 10;
  32.                                 break;
  33.  
  34.                         case 'V':
  35.                                 decimal += 5;
  36.                                 break;
  37.  
  38.                         case 'I':
  39.                                 decimal += 1;
  40.                                 break;
  41.                         }
  42.                         x++;
  43.                 } while (x < romanNumeral.length());
  44.                 System.out.println("Decimal Number is: " + decimal);
  45.         }
  46. }
Feb 29 '12 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Dot Net Daddy | last post: by
31 posts views Thread by ARMAS | last post: by
5 posts views Thread by ria3 | last post: by
7 posts views Thread by billbaitsg | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.