| | |
help with program to convert integers to roman numerals
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2004
Posts: 1
Reputation:
Solved Threads: 0
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;
}
}
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
![]() |
Similar Threads
- Need a little help...(Conversion of Arabic Numerals to Roman Numerals(vice-versa)).. (Visual Basic 4 / 5 / 6)
- Help with converting Roman numerals to Integer values (Python)
- roman numerals (C++)
- roman numbers (Pascal and Delphi)
- int to roman numeral help (C++)
- c program convert to tasm (Assembly)
Other Threads in the Java Forum
- Previous Thread: java price is right game help
- Next Thread: calling methods.
| Thread Tools | Search this Thread |
actuate android api applet application applications array arrays automation balls bank binary bluetooth business chat class classes clear client code codesnippet collections component database defaultmethod development dice dragging ebook eclipse error event exception formatingtextintooltipjava fractal froglogic game givemetehcodez graphics gui hql html ide image infinite input integer intersect invokingapacheantprogrammatically j2me java javaprojects jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie numbers openjavafx parameter php print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting sql sqlserver state storm string sun superclass swing swt text-file threads time tree windows





