Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 roman numeral special cases
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
flameZero




PostPosted: Tue Mar 28, 2006 8:50 am   Post subject: roman numeral special cases

hey guys i was wonderin if u could help me out.
i am doing a roman numeral assignment and i am stuck on the special cases ( IX,IC,IV,etc) i have them in a FOR loop and i tested it out by outputtin "hello" before the calculations but the program doesnt see the fact that IX is 9 instead of 11.i have included my java file.
code:
import javax.swing.JOptionPane;
 import java.io.*;
public class Assignment5c {
public static void main(String[] args) {
       
        String romannum;
        int romantotal;
        int x;
        int Y;
        int lengthOfX;
        String substring=" ";
        char quit='y';
        int i;
        while (quit=='y'){
        romantotal=0;
        romannum=Keyin.inString("enter a roman numeral:");
        lengthOfX = romannum.length();
        for ( i=0; i<lengthOfX;i=i+1){
         Y = romannum.charAt(i);
        int asciivalue=(int)Y;
        switch (asciivalue){
        case 73:
        romantotal+=1;break;
        case 67:
        romantotal+=100;break;
        case 88:
        romantotal+=10;break;
        case 76:
        romantotal+=50;break;
        case 86:
        romantotal+=5;break;
        case 68:
        romantotal+=500;break;
        case 77:
        romantotal+=1000;break;
}
}
for(i=0;i<lengthOfX-1;i+=1){
        substring=romannum.substring(i,i+1);
        System.out.println ("hello");
                if(substring.equals("IV"))
                        romantotal-=2;
                if(substring.equals("IX"))
                        romantotal-=2;
                if(substring.equals("IC"))
                        romantotal-=2;
                if(substring.equals("IL"))
                        romantotal-=2;
                if(substring.equals("ID"))
                        romantotal-=2;
                if(substring.equals("IM"))
                        romantotal-=2;
                if(substring.equals("XL"))
                        romantotal-=20;
                if(substring.equals("XC"))
                        romantotal-=20;
                if(substring.equals("XD"))
                        romantotal-=20;
                if(substring.equals("XM"))
                        romantotal-=20;
                if(substring.equals("CD"))
                        romantotal-=200;
                if(substring.equals("CM"))
                        romantotal-=200;       
       
       
        }
JOptionPane.showMessageDialog(null,"the roman numeral entered was: "+romannum+" the total is: " + romantotal);

quit=Keyin.inChar("would you like to try again?(y/n):");
}
}
}

can you help me see what i did wrong. i believe it is a logic error but i cant figure it out . thanks in advance for any tips.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: