Results 1 to 6 of 6
Thread: Please help
- 12-10-2010, 01:14 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Please help
I was tasked to create a program which would convert arabic numerals to roman numerals and roman numerals to arabic numerals. I have successfully created the arabic to roman but my code for roman to arabic wont work... i think the logic i used was correct, but thats just me. can someone help?
Main
_____________________
import javax.swing.JOptionPane;
import java.lang.NumberFormatException;
import java.lang.NullPointerException;
public class Main {
public static void main(String[] args) {
RomanNumerals rn = new RomanNumerals();
ArabicNumerals an = new ArabicNumerals();
long x = 0;
boolean romanErr = false;
JOptionPane.showMessageDialog(null,"This program will allow you to convert roman numerals to arabic numerals and vice versa","Welcome!",JOptionPane.PLAIN_MESSAGE);
Object[] choice = {"Convert roman numerals to arabic numerals","Convert arabic numerals to roman numerals","Quit"};
int a = JOptionPane.showOptionDialog(null, " " +
"What would you like to do?","Converter",JOptionPane.DEFAULT_OPTION,JOptio nPane.QUESTION_MESSAGE,null,choice,choice[0]);
if(a == JOptionPane.YES_OPTION){
JOptionPane.showMessageDialog(null,"You are only allowed to enter the following letters:\nM, D, C, L, X, V, I","Instructions",JOptionPane.PLAIN_MESSAGE);
String y = JOptionPane.showInputDialog(null,"Enter numerals to be converted","Roman to Arabic",JOptionPane.PLAIN_MESSAGE);
an.setNumerals(y);
JOptionPane.showMessageDialog(null,y + " in arabic numeral is " + an.getNumerals());
}
if(a == JOptionPane.NO_OPTION){
JOptionPane.showMessageDialog(null,"You are allowed to enter numbers from 1 to 3999\nAfter entering an input press OK","Instructions",JOptionPane.PLAIN_MESSAGE);
x = Long.parseLong(JOptionPane.showInputDialog(null,"E nter the number to be converted","Arabic to Roman",JOptionPane.PLAIN_MESSAGE));
rn.setNumerals(x);
JOptionPane.showMessageDialog(null,x + " in roman numerals is " + rn.getNumerals());
}
}
}
_______________________
ArabicNumerals
_______________________
import java.lang.StringIndexOutOfBoundsException;
public class ArabicNumerals {
private char store;
private int a, b, c;
private long fConvertion[] = new long[b+1],convertion;
public void setNumerals(String numerals){
int b = numerals.length();
for(int a= 0;a <= b;a++){
store = numerals.charAt(a);
if(store == 'M'){
fConvertion[a] = 1000;
}
if(store == 'D'){
fConvertion[a] = 500;
}
else if(store == 'C'){
fConvertion[a] = 100;
}
else if(store == 'L'){
fConvertion[a] = 50;
}
else if(store == 'X'){
fConvertion[a] = 10;
}
else if(store == 'V'){
fConvertion[a] = 5;
}
else if(store == 'I'){
fConvertion[a] = 1;
}
}
for(a = 0, c = 1; a<=b ;a++, c++){
if(fConvertion[a] < fConvertion[c] && fConvertion[c] < b){
convertion = convertion + fConvertion[a] - fConvertion[c];
}
else if(fConvertion[a] >= fConvertion[c] && fConvertion[c] < b){
convertion = convertion + fConvertion[a] +fConvertion[c];
}
if(c == b){
c--;
}
}
}
public long getNumerals(){
return convertion;
}
}
_______________________________
its driving me quite mad i dont know what to do with it
- 12-10-2010, 01:19 AM #2
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
the other class
public class RomanNumerals {
private String convertion = "";
public void setNumerals(long numerals){
do{
if(numerals >= 1000){
do{
numerals = numerals - 1000;
convertion = convertion + "M";
}while(numerals >= 1000);
}
else if( numerals >= 900){
do{
numerals = numerals - 900;
convertion = convertion + "CM";
}while(numerals >= 900);
}
else if(numerals >= 500){
do{
numerals = numerals - 500;
convertion = convertion + "D";
}while(numerals >= 500);
}
else if(numerals >= 400){
do{
numerals = numerals - 400;
convertion = convertion + "CD";
}while(numerals >= 400);
}
else if(numerals >= 100){
do{
numerals = numerals - 100;
convertion = convertion + "C";
}while(numerals >= 100);
}
else if(numerals >= 90){
do{
numerals = numerals - 90;
convertion = convertion + "XC";
}while(numerals >= 90);
}
else if(numerals >= 50){
do{
numerals = numerals - 50;
convertion = convertion + "L";
}while(numerals >= 50);
}
else if(numerals >= 40){
do{
numerals = numerals - 40;
convertion = convertion + "XL";
}while(numerals >= 40);
}
else if(numerals >= 10){
do{
numerals = numerals - 10;
convertion = convertion + "X";
}while(numerals >= 10);
}
else if(numerals >= 9){
do{
numerals = numerals - 9;
convertion = convertion + "IX";
}while(numerals >= 9);
}
else if(numerals >= 5){
do{
numerals = numerals - 5;
convertion = convertion + "V";
}while(numerals >= 5);
}
else if(numerals >= 4){
do{
numerals = numerals - 4;
convertion = convertion + "IV";
}while(numerals >= 4);
}
else if(numerals >= 1){
do{
numerals = numerals - 1;
convertion = convertion + "I";
}while(numerals >= 1);
}
this.convertion = convertion;
}while(numerals != 0);
}
public String getNumerals(){
return convertion;
}
}
just figured you need this to run main
- 12-10-2010, 01:41 AM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
See if you can write out your algorithm in pseudocode. It's rather a tricky algorithm, so make sure you have it clearly mapped out before you start coding. I think you'll find it makes more sense if you work from right to left. As you look at each character, if it's the same or higher value than the last one you looked at, add it to your result. If the characters is a lower value than the last one you looked at, subtract it from the result. Once you've seen a character of a higher value, you should only drop down in value for at most one character, and I should only drop down one value level.
For example, consider
Start with the right-most I. It's the first character, so I add its value, 1, to the result.Java Code:MCMXLVIII:
I look at the next I, and it's the same value, so I add another 1. I do the same for the next I.Java Code:Result = 1
Now I look at the V. Its value is higher than the I a just looked at, so I add 5 to the result.Java Code:Result = 3
Now I look at the L. Its value is higher than V, so I add 50.Java Code:Result = 8
Now I look at the X. Its value is lower than the L I previously saw, so I subtract 10.Java Code:Result = 58
I think you get the idea. Make sure you program in the error condition, in case your user enters something illegal like IMCIXLIVJava Code:Result = 48
-Gary-
- 12-10-2010, 02:12 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Please use code tags next time when you are posting over here. Unformated codes are really hard to read. If you don't know how to do that, check on my forum signature.
- 12-10-2010, 03:08 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 3
- Rep Power
- 0
Yeah found a way to get around using my old logic (Yay!)
thanks for the help ^^
the revised code please tell me if the conversions are still wrong
Java Code:import java.lang.StringIndexOutOfBoundsException; public class ArabicNumerals { private char store; private int a, b, c; private long fConvertion[] = new long[9],convertion; public void setNumerals(String numerals){ int b = numerals.length(); for(int a= 0;a <= b-1;a++){ store = numerals.charAt(a); if(store == 'M'){ fConvertion[a] = 1000; } if(store == 'D'){ fConvertion[a] = 500; } else if(store == 'C'){ fConvertion[a] = 100; } else if(store == 'L'){ fConvertion[a] = 50; } else if(store == 'X'){ fConvertion[a] = 10; } else if(store == 'V'){ fConvertion[a] = 5; } else if(store == 'I'){ fConvertion[a] = 1; } } if(b>1){ for(a = 0, c = 1; a <= b ;a++,c++){ if(fConvertion[a] < fConvertion[c]){ convertion = convertion + fConvertion[c] - fConvertion[a]; a++; c++; } else if(fConvertion[a] >= fConvertion[c]){ convertion = convertion + fConvertion[a] +fConvertion[c]; a++; c++; } if(c >= b){ c--; fConvertion[c+1] = 0; } } } else if(b == 1){ for(a = 0, c = 1; a <= b-1 ;a++, c++){ convertion = fConvertion[a]; } } } public long getNumerals(){ return convertion; } }
- 12-13-2010, 04:52 PM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Seems things are fine. Did you comes up with any errors or mismatches in input-output?


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks