DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2004

    problem with Java homework...

    Ok, the problem here is the assignment asks me to create a program that asks the user to either enter a number and convert it to a roman numeral or convert a roman numeral to an int.

    What I've done so far is to set up a basic menu that allows the user to choose which operation they want. So for example lets say they want to convert a roman numeral to a number...

    I have the input set up to take a user inputted string, convert it to upper case and then I'm stuck.

    I'm confused as to how to take the user entered string and convert it into an integer.

    Lets say for example the user entered the roman numerals MCXIV. The program needs to spit out 1114.

    The only thing that I can think to do would be if statements but that would be impossible because theres an infinite amount of combinations.

    Any help would be greatly appreciated.

  2. #2
    Join Date
    Sep 2004
    ok, keep in mind that each roman numeral has its own value, like M is 1000 and X is 10 so on, (i think... )

    So, what you need to do is to split each letter up individually, then do checks on each one, if it is an M make some variable, + 1000,

    ie. if the user enters XI (ie. 11)

    split it up into 2 letters X and I, then make a variable called "total" or something, then do checks on the 2 letters:
    total += 10;
    total += 1;

    and so on...so you would need to loop through all of the letters checking each one
    you get me?
    A kram a day keeps the doctor......guessing

  3. #3
    Join Date
    Sep 2004
    Thanks man

    Although I figured it out on my own about an hour ago just the way you described.

    Also, there is an easier way than writing out a bunch of if statements.

    The example I found is here

    Hopefully all of this will help someone else.

    Thanks again

    P.S. Just for reference...
    String A=("1,000,000");
    String B=("500,000");
    String H=("100,000");
    String F=("50,000");
    String Z=("10,000");
    String Y=("5,000");
    String M = ("1000");
    String CM = ("900");
    String D = ("500");
    String CD =("400");
    String C = ("100");
    String XC = ("90");
    String L = ("50");
    String XL = ("40");
    String X = ("10");
    String IX = ("9");
    String V = ("5");
    String IV = ("4");
    String I = ("1");

    Strings A,B,F,H,Z, and Y are the extended set of roman numerals. Normally you would only see M down through I.

  4. #4
    Join Date
    Sep 2004
    nice work!
    A kram a day keeps the doctor......guessing

  5. #5
    Join Date
    Sep 2004
    Since I'm an open source kind of guy I thought it only relevant to post my code. I'm not posting the code for "main" though because if someone should copy this, they should do some work and learn something....


    public class roman {

    int length;
    int romtonum;
    String roman;
    String num;
    String romnum;

    //Roman to Integer
    public void conv1 (String num)
    int i;
    for (i=0;i < num.length();i++)
    if (num.charAt(i)=='A')
    if (num.charAt(i)=='B')
    if (num.charAt(i)=='H')
    if (num.charAt(i)=='F')
    if (num.charAt(i)=='Z')
    if (num.charAt(i)=='Y')
    if (num.charAt(i)=='M')
    if (num.charAt(i)=='D')
    if (num.charAt(i)=='C')
    if (num.charAt(i)=='L')
    if (num.charAt(i)=='X')
    if (num.charAt(i)=='V')
    if (num.charAt(i)=='I')


    System.out.println("\nIn roman numerals you entered " +romtonum+ "\n");
    }//End Conv1

    //Integer to Roman
    public void conv2 (int rom)
    while (rom >= 1000000) {
    roman += "A";
    rom -= 1000000;
    while (rom >= 500000) {
    roman += "B";
    rom -= 500000;
    while (rom >= 100000) {
    roman += "H";
    rom -= 100000;
    while (rom >= 50000) {
    roman += "F";
    rom -= 50000;
    while (rom >= 10000) {
    roman += "Z";
    rom -= 10000;
    while (rom >= 5000) {
    roman += "Y";
    rom -= 5000;
    while (rom >= 1000) {
    roman += "M";
    rom -= 1000;
    while (rom >= 900) {
    roman += "CM";
    rom -= 900;
    while (rom >= 500) {
    roman += "D";
    rom -= 500;
    while (rom >= 400) {
    roman += "CD";
    rom -= 400;
    while (rom >= 100) {
    roman += "C";
    rom -= 100;
    while (rom >= 90) {
    roman += "XC";
    rom -= 90;
    while (rom >= 50) {
    roman += "L";
    rom -= 50;
    while (rom >= 40) {
    roman += "XL";
    rom -= 40;
    while (rom >= 10) {
    roman += "X";
    rom -= 10;
    while (rom >= 9) {
    roman += "IX";
    rom -= 9;
    while (rom >= 5) {
    roman += "V";
    rom -= 5;
    while (rom >= 4) {
    roman += "IV";
    rom -= 4;
    while (rom >= 1) {
    roman += "I";
    rom -= 1;
    System.out.println("\nYou entered " +roman+ "in roman numerals\n");
    }//END CONV2

    /** Creates new roman */
    public roman()

    }// End Default Constructor

    }// End Class Roman

    /*END CODE*/

  6. #6
    Join Date
    Nov 2004
    Ehhh... it doesn't seem to work. I will not rule out the possibility that I have goofed here but until that dawns upon me I have a few remarks;

    In your roman.conv1(String num) method I have added one line:

    public void conv1 (String num) {
    int i;
    romtonum=0; /* <- uuups (if not they keep getting bigger for each call to this moethod, unless you reinstatiate a new roman instance for each conversion */

    I ran your and my code and here is the result:

    (my code output)
    value for:MMMXXXI: 3031
    value for:MMMXLXI: 3051
    value for:CMXXI: 921
    value for:XL: 40

    (your output, I added the input roman literals to ypur code...)
    In Roman numerals(MMMXLXI) you entered 3071
    In Roman numerals(MMMXXXI) you entered 3031
    In Roman numerals(CMXXI) you entered 1121
    In Roman numerals(XL) you entered 60

    To be honest I dont fancy code in the style you have entered here (no offense), so here is my solution (I have renamed your roman to Roman in the spirit of Java....)

    The clue is to 'nibble' out the double character codes first and then the single character codes while you sum up the total.

    import java.util.*;

    public class RomanNumber {
    public final static Hashtable letter2NumberHt=new Hashtable();
    static {
    letter2NumberHt.put("A", new Integer(1000000));
    letter2NumberHt.put("B", new Integer(500000));
    letter2NumberHt.put("H", new Integer(100000));
    letter2NumberHt.put("F", new Integer(50000));
    letter2NumberHt.put("Z", new Integer(10000));
    letter2NumberHt.put("Y", new Integer(5000));
    letter2NumberHt.put("M", new Integer(1000));
    letter2NumberHt.put("CM", new Integer(900));
    letter2NumberHt.put("D", new Integer(500));
    letter2NumberHt.put("CD", new Integer(400));
    letter2NumberHt.put("C", new Integer(100));
    letter2NumberHt.put("XC", new Integer(90));
    letter2NumberHt.put("L", new Integer(50));
    letter2NumberHt.put("XL", new Integer(40));
    letter2NumberHt.put("X", new Integer(10));
    letter2NumberHt.put("IX", new Integer(9));
    letter2NumberHt.put("V", new Integer(5));
    letter2NumberHt.put("IV", new Integer(4));
    letter2NumberHt.put("I", new Integer(1));
    private String rNString=null;

    private long calculateCharSum (int rcNumLen) {
    long aSum=0;
    Enumeration en=letter2NumberHt.keys();
    while (en.hasMoreElements()) {
    String rc = (String)en.nextElement();
    if (rc.length()!=rcNumLen) continue;
    while(true) {
    int ix=rNString.indexOf(rc);
    if (ix < 0) break;
    return aSum;
    public long getRomanNumberValue(String rNString) {
    long sum=0;
    System.out.println("value for:"+ rNString+": "+sum);
    return sum;
    public RomanNumber() {}
    public static void main(String[] args) {
    RomanNumber rn = new RomanNumber();
    long value=0;
    Roman roman=new Roman();
    eschew obfuscation

  7. #7
    Join Date
    Sep 2004
    I see what you mean. Of course your code if more efficient than mine, however some of the things you used in your code, my class hasn't covered yet. So really, my code is only based on what I know so far. I do appreciate the correction though, how else would I learn?


  8. #8
    Join Date
    Feb 2004
    The problem with your code is that you have the roman numeral system wrong (I think). That is why it is outputing 60 for XL instead of 40.

    You can't simply add all the letters up to get the total, because that isn't how roman numerals work. There is also subtracting that needs to be done. If a letter precedes a letter of greater value then you have to take the first letter from the second one. For example 4 is represented by IV, which is read as 5-1. You need to factor that in.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.