-
Roman Numerals help
I have to convert Roman Numerals to a Decimal Number, I got how to do it, but don't know to restrict somethings,
There is a rule that one symbol cannot repeat more than three times at once, so XIIII should not work, instead it should be XIV, but I do not know how to do this.
I put an if statement within the for loop in class Translate, method numeral, but it does not work.
Code:
import java.io.*;
import java.util.*;
public class RomanNumeral
{
public static void main (String[] args) throws IOException
{
BufferedReader objReader = new BufferedReader (new InputStreamReader (System.in));
for (;;)
{
System.out.println();
System.out.println("Enter roman numeral numbers:");
String romanNumeral = objReader.readLine();
Translate arabNumeral = new Translate(romanNumeral);
String finalOut = arabNumeral.getNumeral();
System.out.println("The conversion is:");
System.out.println(finalOut);
}
}
}
class Translate
{
private String word, roman, result;
private int length, addition = 0;
Translate(String roman)
{
this.roman = roman;
numeral();
}
public void numeral()
{
Scanner conversion = new Scanner(roman);
StringBuffer combine = new StringBuffer ("");
while (conversion.hasNext())
{
word = conversion.next();
length = word.length();
for (int i = 0; i < length-1;i++)
{
if (word.charAt(i) == 'I' && (word.charAt(i+1) == 'V' || word.charAt(i+1) == 'X' || word.charAt(i+1) == 'L' || word.charAt(i+1) == 'C' || word.charAt(i+1) == 'D' || word.charAt(i+1) == 'M'))
{
addition--;
}
else if (word.charAt(i) == 'I' && word.charAt(i+1) == 'I')
{
addition++;
}
else if (word.charAt(i) == 'V' && (word.charAt(i+1) == 'X' || word.charAt(i+1) == 'L' || word.charAt(i+1) == 'C' || word.charAt(i+1) == 'D' || word.charAt(i+1) == 'M'))
{
addition -= 5;
}
else if (word.charAt(i) == 'V' && (word.charAt(i+1) == 'I' || word.charAt(i+1) == 'V'))
{
addition += 5;
}
else if (word.charAt(i) == 'X' && (word.charAt(i+1) == 'L' || word.charAt(i+1) == 'C' || word.charAt(i+1) == 'D' || word.charAt(i+1) == 'M'))
{
addition -= 10;
}
else if (word.charAt(i) == 'X' && (word.charAt(i+1) == 'I' || word.charAt(i+1) == 'V' || word.charAt(i+1) == 'X'))
{
addition += 10;
}
else if (word.charAt(i) == 'L' && (word.charAt(i+1) == 'C' || word.charAt(i+1) == 'D' || word.charAt(i+1) == 'M'))
{
addition -= 50;
}
else if (word.charAt(i) == 'L' && (word.charAt(i+1) == 'I' || word.charAt(i+1) == 'V' || word.charAt(i+1) == 'X' || word.charAt(i+1) == 'L'))
{
addition += 50;
}
else if (word.charAt(i) == 'C' && (word.charAt(i+1) == 'D' || word.charAt(i+1) == 'M'))
{
addition -= 100;
}
else if (word.charAt(i) == 'C' && (word.charAt(i+1) == 'I' || word.charAt(i+1) == 'V' || word.charAt(i+1) == 'X' || word.charAt(i+1) == 'L' || word.charAt(i+1) == 'C'))
{
addition += 100;
}
else if (word.charAt(i) == 'D' && (word.charAt(i+1) == 'M'))
{
addition -= 500;
}
else if (word.charAt(i) == 'D' && (word.charAt(i+1) == 'I' || word.charAt(i+1) == 'V' || word.charAt(i+1) == 'X' || word.charAt(i+1) == 'L' || word.charAt(i+1) == 'C' || word.charAt(i+1) == 'D'))
{
addition += 500;
}
else if (word.charAt(i) == 'M' && (word.charAt(i+1) == 'I' || word.charAt(i+1) == 'V' || word.charAt(i+1) == 'X' || word.charAt(i+1) == 'L' || word.charAt(i+1) == 'C' || word.charAt(i+1) == 'D' || word.charAt(i+1) == 'M'))
{
addition += 1000;
}
}
if (word.charAt(length-1) == 'I')
{
addition++;
}
else if (word.charAt(length-1) == 'V')
{
addition += 5;
}
else if (word.charAt(length-1) == 'X')
{
addition += 10;
}
else if (word.charAt(length-1) == 'L')
{
addition += 50;
}
else if (word.charAt(length-1) == 'C')
{
addition += 100;
}
else if (word.charAt(length-1) == 'D')
{
addition += 500;
}
else if (word.charAt(length-1) == 'M')
{
addition += 1000;
}
combine.append(addition);
combine.append(' ');
addition = 0;
}
result = combine.toString ();
}
public String getNumeral()
{
return result;
}
}
-
Re: Roman Numerals help
-
Re: Roman Numerals help