File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Arabic Numeral Program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arabic Numeral Program" Watch "Arabic Numeral Program" New topic
Author

Arabic Numeral Program

Frank White
Greenhorn

Joined: Mar 01, 2008
Posts: 7
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 ]
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31079
    
163

Can you show what you have so far?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Frank White
Greenhorn

Joined: Mar 01, 2008
Posts: 7
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
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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

Joined: Mar 01, 2008
Posts: 7
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arabic Numeral Program