Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

I need help with converting Roman Numerals to Arabic numbers

807603Dec 10 2007 — edited Dec 11 2007
Hello everyone,

I am taking an AP Computer Science A class and I am doing an assignment to make a program that converts Roman Numerals to Arabic numbers and the opposite. I got the Arabic to Roman part down perfectly, but the other way around is giving me trouble. Can anyone help me?

Here's what I have so far, and the question I have is at the end.

import java.io.*;

public class RomanNumerals{

public static void main (String [] args)
{
DataInput keyboard=new DataInputStream (System.in);
String input;

try
{
//options
System.out.println("1. Roman numerals to Arabic numbers");
System.out.println("2. Arabic numbers to Roman numerals");
System.out.println("3. Exit");
System.out.print("Enter your option: ");
input=keyboard.readLine();

int choice=Integer.parseInt(input);

switch (choice)
{
//Roman numerals to Arabic numbers
case 1:
String romanInput, ro;
int answer1;

System.out.print("Enter a Roman numeral: ");
romanInput=keyboard.readLine();

ro=romanInput.toUpperCase();

answer1=toArabic(ro);

System.out.println("The Arabic number is: "+answer1);
break;
//Arabic numbers to Roman numerals
case 2:
String arabicInput, answer;

System.out.print("Enter an Arabic number: ");
arabicInput=keyboard.readLine();
int arabic=Integer.parseInt(arabicInput);

answer=toRomans(arabic);
System.out.println("The Roman numeral is: "+answer);
break;
case 3:
break;
default:
System.out.println("Invalid option.");
}
}
catch(IOException e)
{
System.out.println("Error");
}
//method to convert Arabic numbers to Roman numerals
}
public static String toRomans (int N)
{
String roman="";

while (N>=1000)
{
roman+="M";
N-=1000;
}
while (N>=900)
{
roman+="CM";
N-=900;
}
while (N>=500)
{
roman+="D";
N-=500;
}
while (N>=400)
{
roman+="CD";
N-=400;
}
while (N>=100)
{
roman+="C";
N-=100;
}
while (N>=90)
{
roman+="XC";
N-=90;
}
while (N>=50)
{
roman+="L";
N-=50;
}
while (N>=40)
{
roman+="XL";
N-=40;
}
while (N>=10)
{
roman+="X";
N-=10;
}
while (N>=9)
{
roman+="IX";
N-=9;
}
while (N>=5)
{
roman+="V";
N-=5;
}
while (N>=4)
{
roman+="IV";
N-=4;
}
while (N>=1)
{
roman+="I";
N-=1;
}

return(roman);
}

//method to convert Roman numerals to Arabic numbers
public static int toArabic (String x)
{
int arabica=0;
char chars;

for (int y=0; y<=(x.length()-1); y++)
{
chars=x.charAt(y);

switch (chars)
{
case 'C':
if (x.charAt(y+1)=='M')
{
arabica+=900;
y++;
}
else if (x.charAt(y+1)=='D')
{
arabica+=400;
y++;
}
else
{
arabica+=100;
}
break;
case 'X':
if (x.charAt(y+1)=='C')
{
arabica+=90;
y++;
}
else if (x.charAt(y+1)=='L')
{
arabica+=40;
y++;
}
else
{
arabica+=10;
}
break;
case 'I':
if (x.charAt(y+1)=='X')
{
arabica+=9;
y++;
}
else if (x.charAt(y+1)=='V')
{
arabica+=4;
y++;
}
else
{
arabica++;
}
break;
case 'M':
arabica+=1000;
break;
case 'D':
arabica+=500;
break;
case 'L':
arabica+=50;
break;
case 'V':
arabica+=5;
break;
}
}
return(arabica);
}
}

I have if-else statements made for each cases for Roman to Arabic. However, when I put the input as any single character C, X, and I, the program will not show a value. (Like if I put just X in, it should show me 10 but it says error. NOTE: single character does work for rest) I'm thinking that something is up with the "else" part of case C, case X, and case I that does not allow me to put in single characters for D, L, and V. What is a possible solution?

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 8 2008
Added on Dec 10 2007
15 comments
6,054 views