1. Senior Member
Join Date
Aug 2013
Location
Sweden
Posts
163
Rep Power
4

## 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 :)

Java 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>();
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);
}
}```

2. ## 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?

3. Senior Member
Join Date
Aug 2013
Location
Sweden
Posts
163
Rep Power
4

## Re: Roman Numerals

Thanks! I did what you said and found the problem :) On line 34 I wrote
Java Code:
`number %= numberArray[x];`
Java Code:
`test %= numberArray[x];`

4. Just a guy
Join Date
Jun 2013
Location
Netherlands
Posts
5,114
Rep Power
9

## Re: Roman Numerals

Good job.

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

Java Code:
`int numberArray[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };`

5. Senior Member
Join Date
Aug 2013
Location
Sweden
Posts
163
Rep Power
4

## Re: Roman Numerals

Thanks for the tip! :) Is there a similar way for the
Java Code:
`Vector<String>`
type?

6. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,659
Rep Power
9

## Re: Roman Numerals

Java Code:
`romanNumerals.addAll(Arrays.asList("M", "CM", "D", .....));`
Regards,
Jim
Last edited by jim829; 09-10-2013 at 07:07 PM.

7. Senior Member
Join Date
Aug 2013
Location
Sweden
Posts
163
Rep Power
4

## Re: Roman Numerals

Thanks Jim! :)
But your suggestion didn't work :(
The debugger just says "Arrays cannot be resolved".
Last edited by Zelaine; 09-10-2013 at 09:02 PM.

8. ## 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

9. Senior Member
Join Date
Aug 2013
Location
Sweden
Posts
163
Rep Power
4

## 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

10. ## 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.

11. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,659
Rep Power
9

## Re: Roman Numerals

Yeah. And the OP even thanked us.

Regards,
Jim
Last edited by jim829; 09-11-2013 at 03:29 AM.

12. ## Re: Roman Numerals

<Fawlty Towers>
A happy customer. We should have them stuffed!
</Fawlty Towers>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•