0

Hello! I'm studying right now as an exchange student in Germany and taking my first programming course in Java, and in the last week, things have just started to go over my head and I'm kind of drowning while trying to get all the homework done!

The assignment was supposed to write a code to convert roman numerals into arabic ones - here's what I have written so far:

```
static int roman2Decimal(String number)
{
int[] numbers = { 1000, 900, 500, 400, 100, 90,
50, 40, 10, 9, 5, 4, 1 }; //it's much easier to program in the values for IV, IX, etc
String[] letters = { "M", "CM", "D", "CD", "C", "XC",
"L", "XL", "X", "IX", "V", "IV", "I" }; //than to use a bunch of 'if's and things to check if a // letter with a lower value is in front of one with a higher one.
final int num;
System.out.println("Please enter a roman numeral using the letters M, D, C, L, X, V, I. All letters will be treated as capitalized.");
System.out.println("Please do not enter a roman numeral with a value greater than 3999."); //3999 is the highest value since we're only
Scanner roman = new Scanner(System.in); //using the roman numerals up to M, and you can't have 4 in a row.
roman = roman.toUpperCase();
roman = roman.trim();
if (roman.length()=0)
{
System.out.println("Please enter an actual number using the above mentioned roman numerals, not just empty spaces.");
roman = input.nextString();
}
int i = 0; //this will be our position in the string of the roman numerals.
int decimal = 0; //this is the decimal value of the roman numerals converted up until this point.
while (i<roman.length())
{
char letter = roman.CharAt(i); //the letter at the position i in the string roman
int number = letterToNumber(letter); //The number that the letters represent - will be found later in the program.
if (number<0)
{
System.out.println("Illegal character " + letter + " in the string you entered. Please try again using ONLY M, D, C, L, X, V, and I.");
roman = input.nextString();
}
if (i == roman.length()) //There are no characters after where we are (at i), so just add the value of that letter to the number.
{
decimal += number;
}
else
{
int nextNumber = letterToNumber(roman.CharAt(i)); //move to look at the next letter in the string
if (nextNumber > number) //if the second number is greater than the one preceeding it, we need to subtract it from the original number.
{
decimal += (nextNumber - number);
i++;
}
else
{
decimal += number;
}
}
} //end while loop
if (decimal > 3999)
{
throw new NumberFormatException("The roman numeral should have a value strictly less than 4000.");
}
num = decimal;
int letterToNumber(char letter)
//This will convert the Roman Numerals to their decimal counterparts - I=1, V=5, etc. and -1 if the character is not a valid Roman Numeral
switch(letter)
{
case I: {return 1; break;
}
case V: {return 5; break;
}
case X: {return 10; break;
}
case L: {return 50; break;
}
case C: {return 100; break;
}
case D: {return 500; break;
}
case M: {return 1000; break;
}
default: {return -1; break;
}
}
public String toString() {
// Return the standard representation of this Roman numeral.
String roman = ""; // The roman numeral.
int N = num; // N represents the part of num that still has
// to be converted to Roman numeral representation.
for (int i = 0; i < numbers.length; i++) {
while (N >= numbers[i]) {
roman += letters[i];
N -= numbers[i];
}
}
return roman;
}
public int toInt() {
// Return the value of this Roman numeral as an int.
return num;
}
```

But I get an error with the int letterToNumber(char letter) (it says I need a semicolon there, which I really don't understand) and I'm not even sure if all of the code would work even if the brackets and semicolons were all in the right place. Am I on the right track?

Thank you in advance for your help!

*Edited
by Ezzaral*: Fixed code tags.