Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Roman Numeral Program

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
I have a program that takes in a string of integers and converts them into roman numerals. This part of the program is complete; however, part 2 of this program is to take in a string of roman numerals and change them to integers. The problem is part2. When I input IIIII it comes out 5. It should read V=5 not IIIII=5. IIIII is not a roman numeral. Is anyone familiar with this problem?

Thank you...Frank White

This is the code I have it will compile and run if you cut and paste.

public class ArabicNumeral extends NumberStringEva {

ArabicNumeral(){
super("IVXLCDM");
} // end of constructor




private int romanValue(char cAry){

switch (cAry){
case 'M':
return 1000;
case 'D':
return 500;
case 'C':
return 100;
case 'L':
return 50;
case 'X':
return 10;
case 'V':
return 5;
case 'I':
return 1;


} // end of romanVlaue

// given a Roman character this method converts it to an integer
// For example Given I returns 1, X returns 10, etc

return -999;
} // end of romanVlaue


/////////////
// STUB ONLY ~ needs code and needs javadoc
/////////////

public String arabicNumeralString (String sTest){
int t;
int ans=0;
/// To upper case method put here See lec 5/
sTest=sTest.toUpperCase();
// What about mixed case?! :-)
t=evalTheString(sTest);
if (t < 0) {
System.out.println("\tArabicNumeral: sTest"+sTest+" is invalid Roman Numeral string");
return null;
}
else {
System.out.println("\tArabicNumeral: sTest"+sTest+" is valid Roman Numeral string");
char cAry[]=sTest.toCharArray();

for (int i=0;i<cAry.length;i++){
cAry[i]=(char)romanValue(cAry[i]);

}
for (int i=0;i<cAry.length;i++) {

if(i<cAry.length-1) {
if(cAry[i] >= cAry[i+1]){
//add to running total
ans= ans + cAry[i];
}
else{

ans= ans - cAry[i];
//sub to running total
}
}
else
ans=ans+cAry[i];
}




/* //1. convert sTest to char array cAry[] -- see Lec 4
char cAry[] = sTest.toCharArray();
// else look ahead to determine if you add or sub the running total (ans)



*/
// your code goes here...could call a private method or three...
//ans=9; // the hard-coded 9 is to allow it to compile...
//
return Integer.toString(ans);
}
} // end of arabicNumeralString


public String toString() {
return ("Arabic Numeral is using "+super.toString());
} // end of toString

} // end of ArabicNumeral
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please don't post the same question twice.
 
    Bookmark Topic Watch Topic
  • New Topic