# Roman Numerals

• 09-10-2013, 04:56 PM
Zelaine
Roman Numerals
I have tried to code a program that lets you enter an integer, and then converts it to a Roman numeral. But it didn't work out that well, some numbers, like 10, 30 or 500, for some reason get really messed up, but some numbers work perfectly. If I for instance enter 10, I get "XIXI", which clearly is wrong. Could you guys take a look at my code and try to find the problem? Because that would be really helpful :)

Code:

```import java.util.Scanner; import java.util.Vector; public class Test{         public static void main(String[] args){                 Scanner hej = new Scanner(System.in);                 Vector<String> romanNumerals = new Vector<String>();                 romanNumerals.add("M"); romanNumerals.add("CM"); romanNumerals.add("D"); romanNumerals.add("CD"); romanNumerals.add("C");                 romanNumerals.add("XC"); romanNumerals.add("L"); romanNumerals.add("XL"); romanNumerals.add("X"); romanNumerals.add("IX");                 romanNumerals.add("V"); romanNumerals.add("IV"); romanNumerals.add("I");                 int numberArray[] = new int[13];                 numberArray[0] = 1000; numberArray[1] = 900; numberArray[2] = 500; numberArray[3] = 400; numberArray[4] = 100;                 numberArray[5] = 90; numberArray[6] = 50; numberArray[7] = 40; numberArray[8] = 10; numberArray[9] = 9;                 numberArray[10] = 5; numberArray[11] = 4; numberArray[12] = 1;                 String result = "";                 System.out.print("Enter an integer from 1 to 1000: ");                 int number = hej.nextInt();                 for(int x=0;x<romanNumerals.size();x++){                         if(x != 4 && x != 8 && x != 12){                                 if(number/numberArray[x]>=1){                                         result += romanNumerals.get(x);                                         number %= numberArray[x];                                 }                         }else{                                 if(number/numberArray[x]>=1){                                         double test = number;                                         if(test >= 100)                                                 test /= 100;                                         else if(test >= 10)                                                 test /= 10;                                         for(int y=0;y<test;y++){                                                 result += romanNumerals.get(x);                                         }                                         test %= numberArray[x];                                 }                         }                 }                 System.out.println(result);         } }```
• 09-10-2013, 04:58 PM
KevinWorkman
Re: Roman Numerals
Have you stepped through this with a debugger, or at least added some print statements, to help figure out what's going on?
• 09-10-2013, 05:07 PM
Zelaine
Re: Roman Numerals
Thanks! I did what you said and found the problem :) On line 34 I wrote
Code:

`number %= numberArray[x];`
Code:

`test %= numberArray[x];`
• 09-10-2013, 05:11 PM
gimbal2
Re: Roman Numerals
Good job.

Note that you can save a bunch of ugly initialization code by initializing the array like this:

Code:

`int numberArray[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };`
• 09-10-2013, 06:46 PM
Zelaine
Re: Roman Numerals
Thanks for the tip! :) Is there a similar way for the
Code:

`Vector<String>`
type?
• 09-10-2013, 07:04 PM
jim829
Re: Roman Numerals
Code:

`romanNumerals.addAll(Arrays.asList("M", "CM", "D", .....));`
Regards,
Jim
• 09-10-2013, 08:54 PM
Zelaine
Re: Roman Numerals
Thanks Jim! :)
But your suggestion didn't work :(
The debugger just says "Arrays cannot be resolved".
• 09-10-2013, 09:51 PM
DarrylBurke
Re: Roman Numerals
You have to import the Arrays class. I'll leave it as an exercise for you to check the API and find what package it's in.

db
• 09-10-2013, 10:53 PM
Zelaine
Re: Roman Numerals
Thanks a lot guys! It works now :) You're so nice for helping me out with this, I mean really really nice
• 09-11-2013, 02:17 AM
KevinWorkman
Re: Roman Numerals
Pretty refreshing to see somebody willing to put in the work and investigate our advice. Nice work OP, you've temporarily restored my faith in humanity.
• 09-11-2013, 03:24 AM
jim829
Re: Roman Numerals
Yeah. And the OP even thanked us.

Regards,
Jim
• 09-11-2013, 04:01 AM
Junky
Re: Roman Numerals
<Fawlty Towers>
A happy customer. We should have them stuffed!
</Fawlty Towers>