# Roman Numeral

Page 1 of 1

## 4 Replies - 28155 Views - Last Post: 14 December 2007 - 01:48 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=22175&amp;s=f9e026a566477b9cf10ccc2de66ad9da&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Felisha

Reputation: 0
• Posts: 9
• Joined: 15-December 06

# Roman Numeral

Posted 17 December 2006 - 02:59 PM

hmm this was kind of a goofy problem and I am not sure if I have it right or if it's going to do what it's suppose to. So can someone PLEASE look over it and tell me want I am missing.. Thanks a bunches.

Here's the problem:

Write a program that converts a number entered in Roman numerals to decimal. Your program should consist of a class say Roman. An object should do the following:

a. Store the number as Roman numeral

b. Convert and store the number into decimal

c. Print the number as Roman numeral or decimal number as requested by the user.

The decimal values of Roman numerals are:
M 1000
D 500
C 100
L 50
X 10
V 5
I 1

```import javax.swing.JOptionPane;

public class CH8EX1
{
public static void main(String[] args)
{
private String numStr = null;
private char ch;
private roman;

public CH8EX1()
{
do
{

numStr = JOptionPane.showinputDialog("Enter a number entered in roman numeral to convert to decimal");
num = interger.parseInt(numStr);

switch(roman)
{
case M: ch = '1000';
break;
case D: ch = '500';
break;
case C: ch = '100';
break;
case L: ch = '50';
break;
case X: ch = '10';
break;
case V: ch = '5';
break;
case I: ch = '1';
break;
}
JOPtionPane.showmessageDialog(null, "Roman Numeral Converted",
JOptionPane.INOFRMATION_MESSAGE);
}
System.exit(0)
}
}

```

Is This A Good Question/Topic? 0

## Replies To: Roman Numeral

### #2 ReggaetonKing

Reputation: 4
• Posts: 73
• Joined: 16-November 06

## Re: Roman Numeral

Posted 18 December 2006 - 09:05 PM

You never specified the data type for the variable 'num'. Also you have the switch statement choose a case according to the 'roman' variable which you also didn't specify the data type nor let the user enter something for it. You also never have a while statement there. Compile the code and you should read your error messages!!

```import javax.swing.JOptionPane;

public class CH8EX1
{
public static void main(String[] args)
{
private String numStr;
private int ch;
private char roman;

public CH8EX1()
{
do
{

numStr = JOptionPane.showInputDialog("Enter a number entered in roman numeral to convert to decimal");
roman = roman.charAt(0); //return a char value from the first character in the string

switch(roman)
{
case 'M':
ch += 1000;
break;
case 'D':
ch += 500;
break;
case 'C':
ch += 100;
break;
case 'L':
ch += 50;
break;
case 'X':
ch += 10;
break;
case 'V':
ch += 5;
break;
case 'I':
ch += 1;
break;
}
}while(true)  //make a condition here...

JOPtionPane.showMessageDialog(null, "Roman Numeral Converted : " + ch,				   JOptionPane.INFORMATION_MESSAGE);
}
System.exit(0)
}
}

```

I got rid of some of the error for ya!

This post has been edited by ReggaetonKing: 18 December 2006 - 09:13 PM

### #3 William_Wilson

• lost in compilation

Reputation: 207
• Posts: 4,812
• Joined: 23-December 05

## Re: Roman Numeral

Posted 18 December 2006 - 09:06 PM

Quote

private roman;

roman has no type, and it is never set to any value, so your switch statement will never work.
you have also set ch to type char, which holds only one letter, not '1000', this also will never work.
i believe what you want to do is make ch an int, then have ch += the value of the switch, ex:
```ch = 0;
switch(roman)
{
case M: ch += 1000;	break;

```

also you need to set roman before the loop, i suggest moving to a for loop based on the length of the text entered, and using the charAt() method to get each char, then you will need to write rules to handle cases such as:
IX = 9

*this assumes you want to handle more than a single letter of input.

### #4 frafri

Reputation: 1
• Posts: 15
• Joined: 12-October 07

## Re: Roman Numeral

Posted 14 December 2007 - 09:50 AM

here is the right program
```public class romannumbers
{

String numStr;
int ch;
char roman;

public void romannumbers1()
{
numStr = JOptionPane.showInputDialog("Enter a number entered in roman numeral to convert to decimal");
int x = 0;
do
{

roman =numStr.charAt(x); //return a char value from the first character in the string

switch(roman)
{
case 'M':
ch += 1000;
break;
case 'D':
ch += 500;
break;
case 'C':
ch += 100;
break;
case 'L':
ch += 50;
break;
case 'X':
ch += 10;
break;
case 'V':
ch += 5;
break;
case 'I':
ch += 1;
break;
}
x++;
}while(x<numStr.length());  //make a condition here...

JOptionPane.showMessageDialog(null, "Roman Numeral Converted :  " + ch);// + ch,				   JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
public static void main(String args[])
{
romannumbers demo = new romannumbers();
demo.romannumbers1();
}
}

```

Use tags

This post has been edited by jayman9: 14 December 2007 - 07:42 PM

### #5 baavgai

• Dreaming Coder

Reputation: 7111
• Posts: 14,826
• Joined: 16-October 07

## Re: Roman Numeral

Posted 14 December 2007 - 01:48 PM

Just to make you aware, most posited code here is missing something. The decimal 9 is not "VIIII", but rather "IX".

Here's a simple test case. MCMLCVX is 1999. If you aren't getting that, the code has issues.

Hope this helps.