• Post Reply 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

Arabic Numeral Program

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written a program to take roman numerals in, and parse them into arabic integers. The problem I'm having is parameters with IIIII=5 and not V. Can anyone help?

public class ArabicNumeral extends NumberStringEva {

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




private int romanValue(char cAry){

switch (cAry){
case 'M':
return1000;
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
[ March 02, 2008: Message edited by: Frankhank White ]
 
author & internet detective
Posts: 41878
909
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show what you have so far?
 
Frank White
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeanne, I posted the cod that I have. Yesterday it kept telling me I have invalid character when posting. It told me to Change all "<" to &B or something but it wouldn't let me post until I took the code out. Sorry.

Frank
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Frankhank White:
The problem I'm having is parameters with IIIII=5 and not V.

I don't get this either. Are you saying when you input "IIIII" you should get "5" but you actually get "V"? Surely not. Maybe you could try again. Tell us what your inputs are, what your outputs should be, and what you are getting instead.
 
Frank White
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for responding, and sorry for not being more clear. This is my first time.

The program is supposed to input (from the command prompt) integers and the program is to change them from arabic to roman numeral. That part I have completed. Also, the program (Part 2) is to be expanded to input roman numerals and output arabic numerals. This is the part I'm having trouble with. I got it to work, but if you input IIIII you get 5, when V=5. IIIII is outside the params it is not a roman numeral.

Thank you Frank
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic