2

Currently making a program that converts integers to Roman Numerals. I don't have much written for my code because I am trying to brainstorm how I can do this before writing much:

import java.util.Scanner;
public class MyClass 
{
    public static void main(String args[]) 
    {


        Scanner n = new Scanner(System.in);
        System.out.println("Please enter an integer and I will convert it to 
        a Roman Numeral:");
        int rn = n.nextInt();

That is all I have right now.

The main question is; how would I assign a certain number to a roman numeral and then output the Roman Numeral that matches the int?

An answer would be appreciated but please do not give me the completed code because I am still a beginner and would like to learn without delays. Thank you!

  • I think this maybe is double -posted here? --> https://stackoverflow.com/questions/12967896/converting-integers-to-roman-numerals-java – XsiSecOfficial Jan 17 '18 at 16:25
  • For the "Roman numeral" I think you have to use a String. You can't store I's and V's in an int, so it'll have to be a String. You could use a char array, but that's less convenient. Also, have a look at `StringBuilder`, which is basically a mutable string for Java. – markspace Jan 17 '18 at 16:30

4 Answers4

1

If there is a finite set of numbers the user can input, then you can create a Map<int, string> to map a number to a roman numeral. Another option, one that would work for any input, would be to create a method that can 'calculate' the roman numeral based off of an integer value.

public static String toRoman(int value) {
    String romanNumeral = "";
     /* ... 
        code to build romanNumeral would go here ...
        (e.g.) for (blah) ... if (blah)  ..... romanNumeral += "I"; ...
     */
    return romanNumeral;
}

From there you should think about what steps you take mentally to convert a number to a roman numeral, and then write code to do those steps

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
0

This isn't an answer to you question, but more of a code review from someone who has been around the block (Java developer for 20 years—zikes!).

Why did you choose n as the variable name for the Scanner? I'd suggest you call it the far more obvious scanner. Short variable names are reasonable if you are dealing with coordinates (x,y,z), or indices (i, j), but should not be the norm.

Use longer names with camelCase and try to be very accurate when you name things. For example, your other variable rn is, I assume, short for Roman numeral. But it is not a Roman numeral. It's a decimal integer that you want to convert into a Roman numeral.

ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66
  • Good point. Again, I am a beginner so constructive criticism of my code is always helpful. I will keep that in mind when writing next time. – StevenDario Jan 18 '18 at 04:55
0

Refer this link, you will get some idea

http://www.guideforschool.com/1930249-program-on-decimal-to-roman-number-conversion-method-2/

Gnana
  • 144
  • 7
  • Good example, but i think OP wants to work it out himself, big part of learning is acquiring the mindset of solving problems on your own. There are just situations where re-inventing the wheel and failing at it is just necessary. – apexlol Jan 17 '18 at 17:56
0

I know you didn't want answers, but I doubt this is the way you will have solved it. So, for your interest:

public static String toRomanNumeral(int i) {
    return ",M,MM,MMM".split(",")[i/1000]
         + ",C,CC,CCC,CD,D,DC,DCC,DCCC,CM".split(",")[i%1000/100]
         + ",X,XX,XXX,XL,L,LX,LXX,LXXX,XC".split(",")[i%100/10]
         + ",I,II,III,IV,V,VI,VII,VIII,IX".split(",")[i%10];
}
ᴇʟᴇvᴀтᴇ
  • 12,285
  • 4
  • 43
  • 66