This is my class for converting integers to roman numerals and roman numerals to integers. When I put a number say, 85, into arabicToRoman, I get back the correct roman numeral, but the integer I get back is always 5 less, so 85 gives 80. and when I used romanToArabic I get back the number subtracted by five and the roman numeral missing the last letter. Can you please help me find the root of the problem? everything is pasted below (I'm in a beginner high school course, be gentle)
public class RomanNumeral
{
private int arabic;
private String roman;
public RomanNumeral(String r)
{
roman = r.toUpperCase();
arabic = romanToArabic();
}
public RomanNumeral(int a)
{
arabic = a;
roman = arabicToRoman();
}
public int romanToArabic()
{
int sum = 0;
int i;
for(i=0; i<roman.length()-1; i++)
{
char rom = roman.charAt(i);
switch(rom)
{
case 'M':
sum += 1000;
break;
case 'D':
sum += 500;
break;
case 'C':
char next = roman.charAt(i+1);
if(next == 'M')
{
sum += 900;
i++;
}
if( next == 'D')
{
sum += 400;
i++;
}
else
{
sum += 100;
}
break;
case 'L':
sum += 50;
break;
case 'X':
next = roman.charAt(i+1);
if(next == 'L')
{
sum += 40;
i++;
}
if(next == 'C')
{
sum += 90;
i++;
}
else
{
sum += 10;
}
break;
case 'V':
next = roman.charAt(i+1);
if(next == 'I')
{
sum += 6;
i++;
}
else
{
sum += 5;
}
break;
case 'I':
next = roman.charAt(i+1);
if(next == 'V')
{
sum += 4;
i++;
}
if(next == 'X')
{
sum += 9;
i++;
}
else
{
sum += 1;
}
break;
default:
System.out.println("I've encountered an issue with the characters you've entered.");
}
}
return sum;
}
public String arabicToRoman()
{
int temp = arabic;
String hud = "";
while(temp >= 1000)
{
hud += "M";
temp -= 1000;
}
while(temp >= 900)
{
hud += "CM";
temp -= 900;
}
while( temp >= 500)
{
hud += "D";
temp -= 500;
}
while( temp >= 400)
{
hud += "CD";
temp -= 400;
}
while(temp >= 100)
{
hud += "C";
temp -= 100;
}
while(temp>= 90)
{
hud += "XC";
temp -= 90;
}
while(temp >= 50)
{
hud += "L";
temp -= 50;
}
while( temp >= 40)
{
hud += "XL";
temp -= 40;
}
while (temp >= 10)
{
hud += "X";
temp -= 10;
}
while( temp >= 9)
{
hud += "IX";
temp -= 9;
}
while(temp >= 6)
{
hud += "VI";
temp -= 6;
}
while (temp >= 5)
{
hud += "V";
temp -= 5;
}
while (temp >= 4)
{
hud += "IV";
temp -= 4;
}
while (temp >= 1)
{
hud += "I";
temp -= 1;
}
return hud;
}
public String toString()
{
String hud = arabicToRoman();
int sum = romanToArabic();
return "The roman numeral equivalent of the number " + sum + " " + "is " + hud;
}
}
85 as a Roman number is LXXXV - put that into your
romanToArabic()
function, and this happens:for
loop with i = 0i<roman.length()-1
is now testing4 < 4
and that's not true, they are equal.It stops too early and misses the last Roman numeral.
You will have to loop one more time, and then rethink this bit:
because when the main loop gets to the last position, these lines will try look past one position over the end of the string, and cause an error message. Instead, you could write them with a safety-check: "if we're not at the last position, get char at position i+1, otherwise skip that bit and handle this character alone".
(This kind of error is known as an off-by-one error, and it's very easy when writing code that counts through things to finish one place too early or overrun by one place).