Roman Numbers

• December 7th, 2009, 04:43 AM
chronoz13
Roman Numbers
help here pls.. i'm having problem on how can i conver a roman number into a real number

heres what i've got so far
Code :

```public class RomanNumbers {   private static BufferedReader br = new BufferedReader(new InputStreamReader( System.in));   public static void main(String[] args) throws IOException {   char temp = 0;   int rN = 0;   int num = 0;   System.out.print("Enter The Roman Number: "); String romanNumber = br.readLine();   for (int count = 0; count <= romanNumber.length() - 1; count++) {   temp = romanNumber.charAt(count);   switch (temp) {   case 'I':   rN = 1;   break;   case 'V':   rN = 5;   break; }   num = num + rN; }   System.out.println(num); } }```

the problem is when i enetered a roman number for example:

IV = 4, because I is less than V which is one of the rules in converting a roman number

if the first number is less than the next one... it should be subtracted... otherwise it will be added...

my problem is how can i compare the first value of the roman number into the value next to it..

i dont have any more problems when the sequence is greatest to least.. because i only have to add the values,,
but im having a hard time thinking of how could i compare the values to get the answer if the first number is less than the next number
• December 7th, 2009, 07:24 AM
Freaky Chris
Re: Roman Numbers
How abou tthis

Code :

```import java.util.Scanner;     public class Roman { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in);   System.out.print("Enter a Roman Number:> "); char[] roman = stdIn.nextLine().toCharArray();   int total = 0; for(int i = roman.length-1; i > -1; i--){ switch(roman[i]){ case 'I': total += value(roman[i]); break; case 'V': case 'X': case 'L': case 'C': case 'D': case 'M': if(i != 0 && (value(roman[i-1]) < value(roman[i]))){ total += value(roman[i]) - value(roman[i-1]); i--; }else{ total += value(roman[i]); } break; } } System.out.println(total); }   public static int value(char c){ switch(c){ case 'I': return 1; case 'V': return 5; case 'X': return 10; case 'L': return 50; case 'C': return 100; case 'D': return 500; case 'M': return 1000; default: return 0; } }   }```

Chris
• December 7th, 2009, 07:57 AM
Json
Re: Roman Numbers
Basically what happens here is that for every character you translate you need to check the next character to see if its bigger than the current character and if it is you know that its a subtraction.

Read the section called "Subtractive principle" on Roman numerals - Wikipedia, the free encyclopedia

Quote:

Generally, Roman numerals are written in descending order from left to right, and are added sequentially, for example MMVI (2006) is interpreted as 1000 + 1000 + 5 + 1.
Certain combinations employ a subtractive principle, which specifies that where a symbol of smaller value precedes a symbol of larger value, the smaller value is subtracted from the larger value, and the result is added to the total. For example, in MCMXLIV (1944), the symbols C, X and I each precede a symbol of higher value, and the result is interpreted as 1000 plus (1000 minus 100) plus (50 minus 10) plus (5 minus 1).
A numeral for 10n (I, X, or C) may not precede a numeral larger than 10n+1, where n is an integer.[citation needed] That is, I may precede V and X, but not L or C; X may precede L or C, but not D or M. The numerals 5×10n (V, L, or D) may not be followed by a numeral of greater or equal value.[citation needed] Any symbol that appears more than once consecutively may not be followed by a symbol of larger value.
// Json
• December 8th, 2009, 03:35 AM
chronoz13
Re: Roman Numbers
chris.. i noticed something in the for statement.... the "length" method , i looks different..
in my IDE (netbeans) it is color green..

and color green, statnds for fields or objects... but i dont know where that "length" came from...
• December 8th, 2009, 05:13 AM
Freaky Chris
Re: Roman Numbers
the variable roman is a char Array, in Java these have a variable known as length containing the size of the array.

Chris
• December 8th, 2009, 05:17 AM
chronoz13
Re: Roman Numbers
ahh so thats it.. a method that returns a length of an array of characters.....

tnx!! :)
• December 8th, 2009, 05:34 AM
Freaky Chris
Re: Roman Numbers
Not a method, an instance variable
• December 8th, 2009, 05:47 AM
chronoz13
Re: Roman Numbers
aw .. sorry....
• December 8th, 2009, 05:53 AM
Freaky Chris
Re: Roman Numbers
Just making sure you now :)

Chris
• December 8th, 2009, 06:11 AM
chronoz13
Re: Roman Numbers
tnx tnx! :):D\m/
• December 8th, 2009, 09:58 AM
chronoz13
Re: Roman Numbers

Code :

``` case 'V': case 'X': case 'L': case 'C': case 'D': case 'M':```

ahmm...

if i pass the characer for example 'V' to the method .value(<char>),
its doest not return ALL the succeding values after the 'V' right?

the return value will just depend on the character that i will pass to that method...?

does my analyzation ryt?

coz im a bit confuse on my first look on your code...

i thought it's just an ordinary sequence of switch... (because i dont see a 'break' statement)

so i thought it will return all the next values after the character that i passed....

• December 8th, 2009, 10:28 AM
Json
Re: Roman Numbers
Basically what happens here is that if you pass in 'V' it will fall through down to the 'M' case and run that code, the same thing happens if you pass in either 'X', 'L', 'C' or 'D'.

You need to take note that switch cases falls through to the next one unless the case has a call to break in it.

Code :

``` switch(roman[i]){ case 'I': total += value(roman[i]); break; // We call break here to break out of the switch case 'V': // No break so fall down to X case 'X': // No break so fall down to L case 'L': // No break so fall down to C case 'C': // No break so fall down to D case 'D': // No break so fall down to M case 'M': if(i != 0 && (value(roman[i-1]) < value(roman[i]))){ total += value(roman[i]) - value(roman[i-1]); i--; }else{ total += value(roman[i]); } break; // BREAK }```

// Json
• December 8th, 2009, 10:33 AM
chronoz13
Re: Roman Numbers
what do you mean by 'fall down' sir?

does it mean it also returns value ? ryt?

in this case... we are only asking for the value of 'V' ... so the value of the rest are not used....

am i ryt sir?
• December 8th, 2009, 12:09 PM
helloworld922
Re: Roman Numbers
The next case statement doesn't stop execution of the previous case statement. So, when he says "fall down", he wants all the code from V...M to all be the same.

Code :

```char c = 'a'; switch(c) { case 'a': System.out.println("The char is definitely an a"); case 'b': System.out.println("The char is either an a or b"); }```

This will print out:

The char is definitely an a
The char is either an a or b

If we take out the first print statement,
Code :

```switch(c) { case 'a': case 'b': System.out.println("The char is either an a or b"); }```

It will only print out:

The char is either an a or b
• December 12th, 2009, 01:19 AM
chronoz13
Re: Roman Numbers
```public class RomanNumbers3 {   private static BufferedReader br = new BufferedReader(new InputStreamReader( System.in));   public static void main(String[] args) throws IOException {   int rN = 0; int equivalent = 0; int[] value;   char[] romanChar = {'I'};   System.out.print("Enter a roman number: "); String romanNumber = br.readLine();   value = new int[romanNumber.length()]; romanChar = new char[romanNumber.length()];   // get the values of each of the roman character for (int x = romanNumber.length() - 1; x >= 0; x--) {   switch (romanNumber.charAt(x)) {   case 'I':   rN = 1; break;   case 'V':   rN = 5; break;   case 'X':   rN = 10; break;   case 'L':   rN = 50; break;   case 'C':   rN = 100; break;   case 'D':   rN = 500; break;   case 'M':   rN = 1000; break;   default:   System.out.println("Invalid Roman Character Sequence"); break; }   // IM HAVING SOME PROBLEM WITH CHECKING OF THE CHARACTERS for (int check = romanChar.length - 1; check >= 0; check--) {   if (romanNumber.charAt(x) != romanChar[0]) {   System.out.println("WRONG CHARACTER"); } }   // assign the value according to the sequence of the characters value[x] = rN; }   for (int n = romanNumber.length() - 1; n >= 0; n--) {   // compute the values according to the sequence of roman characters if ((n != 0) && (value[n] > value[n - 1])) {   equivalent = equivalent + (value[n] - value[n - 1]); n--; } else {   equivalent = equivalent + value[n]; } }   System.out.println(equivalent); } }```