help with program to convert integers to roman numerals

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2004
Posts: 1
Reputation: akilbakari is an unknown quantity at this point 
Solved Threads: 0
akilbakari akilbakari is offline Offline
Newbie Poster

help with program to convert integers to roman numerals

 
0
  #1
Sep 3rd, 2004
does anyone know where i can get source code to write a java 2 program GUI that converts integers to roman numerals? i would appreciate it.
my e-mail address is akilbakari@hotmail.com. by the way, here is my code:
import java.io.*;

public class Roman{

static final int values[]={1,5,10,50,100,500,1000};
static final char letters[]={'I','V','X','L','C','D','M'};

public static void main(String args[]) throws IOException,ArrayIndexOutOfBoundsException{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String numeral="";
int tempVal1=0;
int tempVal2=0;
int ans=0;
char ch1,ch2,ch3,ch4;


System.out.print("Enter a Roman number: ");
numeral=br.readLine();
numeral=numeral+" ";

for(int x=0;x<numeral.length();x++){
ch1=numeral.charAt(x);

if(ch1==' ')
break;

ch2=numeral.charAt(x+1);

tempVal1=findPos(ch1);
tempVal2=findPos(ch2);

if(x==0){
if(values[tempVal1]>values[tempVal2]){
ans+=values[tempVal1];
}
else if(values[tempVal2]>values[tempVal1]){
ans+=(values[tempVal2]-values[tempVal1]);
x+=1;
}
else if(values[tempVal1]==values[tempVal2]){
ans+=values[tempVal1];
ans+=values[tempVal2];
x+=1;
}
}
else{
if(ch2==' '){
ch3=numeral.charAt(numeral.length()-2);
ch4=numeral.charAt(numeral.length()-3);
int tempVal3=findPos(ch3);
int tempVal4=findPos(ch4);

if(values[tempVal3]<=values[tempVal4]){
ans+=values[tempVal3];
}
break;
}
else if(tempVal1<tempVal2){
ans+=(values[tempVal2]-values[tempVal1]);
x+=1;
}
else if(tempVal1>tempVal2){
ans+=values[tempVal1];

}

else if(tempVal1==tempVal2){
ans+=values[tempVal1];
ans+=values[tempVal2];
x++;
}
}

}

System.out.println("Integer value= "+ans);

}


public static int findPos(char ch){
for(int y=0;y<letters.length;y++){
if(ch==letters[y]){
return y;
}
}
return 1;

}
}
Last edited by akilbakari; Sep 3rd, 2004 at 1:59 pm. Reason: forgot my code
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC