3 Replies - 10918 Views - Last Post: 11 February 2011 - 02:09 AM Rate Topic: -----

#1 14th_Doctor   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 10-February 11

Roman Numeral to Decimal Conversion

Posted 10 February 2011 - 11:01 PM

I figured out how to do this using a series of Switch statements. The only problem is with multiple characters if I input VII I will get 7, but if I put in IV, I'll get 6.

I found a piece of code, that I thought steered me the right way, and I added things like 'CM', but now I'm getting an unclosed literal error, because 'CM' is technically a string. So I tried changing char to String, and the switch statement is incompatible.

This makes me think I might have to convert the whole thing into while statements, but before I go about doing that, I was wondering if anyone could look my code over, and see if there is another workaround for it.

Thanks

import java.util.*;

public class RomanArabic {
    public static void main (String args[]) {
        int arabicNumber = 0;
        Scanner input = new Scanner (System.in);
        System.out.print(" Enter a Roman Numeral " );
        String roman = input.next();
        String paxRomana = roman.toUpperCase();
        int x = 0;
        do {
            char convertToArabic = roman.charAt (x);
            switch (convertToArabic) {

                    case 'M':
                    arabicNumber += 1000;
                    break;
                    
                    case 'CM':
                    arabicNumber += 900;
                    break;


                    case 'D':
                    arabicNumber += 500;
                    break;

                    case "CD":
                    arabicNumber += 400;
                    break;

                    case 'C':
                    arabicNumber += 100;
                    break;

                    case "XC":
                    arabicNumber += 90;
                    break;

                    case 'L':
                    arabicNumber += 50;
                    break;

                    case "XL":
                    arabicNumber += 40;
                    break;

                    case 'X':
                    arabicNumber += 10;
                    break;

                    case "IX":
                    arabicNumber += 9;
                    break;

                    case 'V':
                    arabicNumber += 5;
                    break;

                    case "IV":
                    arabicNumber += 4;
                    break;

                    case 'I':
                    arabicNumber += 1;
                    break;

                    }
                    x++;
        } while (x < paxRomana.length());
        System.out.println("The converted number is " + arabicNumber);
    }

}

        


Is This A Good Question/Topic? 0
  • +

Replies To: Roman Numeral to Decimal Conversion

#2 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: Roman Numeral to Decimal Conversion

Posted 11 February 2011 - 01:39 AM

I think strings in switch statements are planned for a future version of Java. That doesn't help you right now but you could use if .. else if ...

You're also going to have to use a different way of splitting up your String. charAt() will only ever return a single character.

This post has been edited by cfoley: 11 February 2011 - 01:39 AM

Was This Post Helpful? 1
  • +
  • -

#3 n8schatten   User is offline

  • D.I.C Regular
  • member icon

Reputation: 146
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: Roman Numeral to Decimal Conversion

Posted 11 February 2011 - 01:47 AM

I would propose using a Map<String, Integer>. This way you can create a LookUp-Table and do not need a massive if-else-cascade. Something like:

Map<String, Integer> lookUp = new HashMap<String, Integer>();
lookUp.put("I", Integer.valueOf(1));
lookUp.put("V", Integer.valueOf(5));
lookUp.put("X", Integer.valueOf(10));
...



Regarding the 'IV'-problem, is that a coding problem or a problem of the rule set of roman numbers?
Was This Post Helpful? 1
  • +
  • -

#4 Syfer   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 190
  • Joined: 08-October 10

Re: Roman Numeral to Decimal Conversion

Posted 11 February 2011 - 02:09 AM

View Postcfoley, on 11 February 2011 - 01:39 AM, said:

I think strings in switch statements are planned for a future version of Java. That doesn't help you right now but you could use if .. else if ...

You're also going to have to use a different way of splitting up your String. charAt() will only ever return a single character.


This would be the best and more simple.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1