Solved

How to convert decimal number to roman number in Java

Posted on 2001-08-10
4
1,573 Views
Last Modified: 2012-05-04
Hi all,

     I want to know how to convert decimal number to roman number and vice-versa.

manohar.

0
Comment
Question by:manu_gmr
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
DidierD earned 25 total points
ID: 6379209
public class Int2Rom {

    /* here goes control arrays for roman numerals */

    /* let: char symbols for each value */
   
    static char[] let = { 'I', 'V', 'X', 'L' };

    /* min: the idea of this array is to determine, how
     * long can we use this numeral, even if value is less
     * then numeral value. e.g. if int==8, then numeral
     * will be 'X'-2, i.e. IIX
     */

    static int[]  min = { 0, 1, 2, 2 };

    /* val: values for numerals */

    static int[]  val = { 1, 5, 10, 50 };

    /* ref: references to numbers we can substract.
     * For 'L' we can substract only 'X', for 'X' and 'V'
     * we can substract only 'I's
     */

    static int[]  ref = { 0, 0, 0, 2 };

    /* values: last index in arrays */
   
    static int values = 3;

    static void usage() {
     System.out.println("Usage: java Int2Rom integer_number");
     System.exit(1);
    }

    public static void main(String args[]) {

     int arg = 0;

     if (args.length < 1) {
         usage();
     }

     try {
         arg = Integer.parseInt(args[0]);
     } catch (Exception e) {
         usage();
     }

     String result = process(arg);

     System.out.println("Integer = "+arg+", Roman = "+result);
     System.exit(0);

    }

    static String process(int x) {

     
     int cval = values; // set index to the last one.
     String res = new String(""); // create empty string

     while (cval >= 0) { // up to all values

         // this while{} will work if we can print let[cval]
         
         while (x >= (val[cval]-(min[cval]*val[ref[cval]]))) {

          System.err.println("x="+x+",max="+
               (val[cval]-(min[cval]*val[ref[cval]]))+
               " == do addRight("+let[cval]+")");
         
          res = addRight(res, let[cval]);

          int recall = 0;

          System.err.println("Prepare loop: "+
               "lmt="+(val[cval]-x)+", step="+val[ref[cval]]);
         
          // this loop will only work if the original value
          // was less than numeral value.
         
          for (int i=0; i<(val[cval]-x);i+=val[ref[cval]]) {
              System.err.println("Incomplete "+(-i+val[cval]-x)+", do"+
                   "addLeft("+let[ref[cval]]+"), i="+i);
              res = addLeft(res, let[ref[cval]]);
              recall++;
          }
         
          // this line reduce original value on the value we
          // already printed
         
          x -= (val[cval]-(recall*val[ref[cval]]));
         }

         cval--;
         
     }

     return res;
     
    }

    /* this method adds character to the end of sourse string */

    static String addRight(String orig, char c) {

     String res = orig.concat(String.valueOf(c));
     return res;
     
    }

    /* this method inserts character before last character of the
     * source string */

    static String addLeft(String orig, char c) {

     char last = orig.charAt(orig.length()-1);

     System.err.print("aL: last="+last);

     String res = orig.substring(0,orig.length()-1);

     System.err.print(", lside="+res+", concat=>"+c+"< and >"+last+"<");

     res = (res.concat(String.valueOf(c))).concat(String.valueOf(last));

     System.err.println(", fin="+res);

     return res;

    }

}

0
 
LVL 2

Expert Comment

by:DidierD
ID: 6379238
Hi,

For converting from roman to decimal number see at http://java.oreilly.com/bite-size/java_0698.html

Didier
0
 

Expert Comment

by:CleanupPing
ID: 9061888
manu_gmr:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 20

Expert Comment

by:Venabili
ID: 9330076
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for DidierD

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 83
more than one jdk and one jre 1 34
even odd program using while loop 3 29
expectj telnet failing 5 26
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now