473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 19272
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

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

Similar topics

25
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual...
6
by: Doohan W. | last post by:
Hi, I'm now working with DB2, and I can't find out how to execute the contents of a string Statement, without using a Java/... procedure, only using SQL statements. I know that some SQBDs such...
2
by: Dot Net Daddy | last post by:
Hello, I cannot get the following Insert Command work. I get the error: Error converting data type varchar to numeric. Description: An unhandled exception occurred during the execution of...
7
by: daming23 | last post by:
Hi, I'm taking an online C++ class and having some difficulty understanding the assignments. Can someone please look at the code I wrote to ensure validity? I am not asking for the answer just some...
31
by: ARMAS | last post by:
Make a program that allows the user to input a number and aoutput it's roman numeral equivalent..... plzzzzzz help.... Ex: 109 output:CIX
5
by: ria3 | last post by:
Hi, I have to write a program that will alow the user to specify a number (decimal, binary, or hexadecimal) and a base to convert to (decimal, binary, or hexadecimal) using subroutine methods and...
7
by: billbaitsg | last post by:
Hi, I need some help with figuring out why my program is all messed up. There are two portions two it, one that converts from roman to decimal (rom2dec) and another that converts from decimal to...
2
by: tonesgirl85 | last post by:
I need some direction to know if I am on the right track. Please contique program below. import java.util.*; public class Roman { static Scanner console = new Scanner(System.in);
7
helpwithcode
by: helpwithcode | last post by:
Hi people, I am just learning java.I have been creating a project which involves JDBC Connectivity.I find that the statements, String string_dob=text_dob.getText(); //Converting string to...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.