# Roman Calculator help!

• 03-15-2013, 02:53 PM
LetsG0Blue
Roman Calculator help!
Hello!

I'm having a lot of issues with my roman calculator. My first issue is that;

Code:

```    char getOperator()     {             System.out.println("Enter operation: + - * / q (q ==> quit) : ");             String input = scan.next();                 return getOperator();     }```
Keeps returning "Enter operation: + - * / q (q ==> quit) :"

and my second issue is neither one of my convert_to_Roman or convert_from_Roman are working.

I keep getting 0 as my answer.

Code:

```import java.util.*; public class RomanCalculator {     // scan can now be used anywhere within this class     Scanner scan = new Scanner(System.in);         // This routine either returns false if the use wants to quit,     //    or it does one Roman Calculator calculation     boolean doCalculation()     {                                 char operand, operator = getOperator();                 if (operator == 'q')                         return false;                                 int operand1 = getOperand(1),operand2 = getOperand(2);                     int answer = doArithmetic(operand1, operand2, operator);                     String sAnswer = convert_to_Roman(answer);                     System.out.println("Answer = " + answer);                             return true;             }         // This routine prompts the user with     // Operator: +  -  *  /  q for quit     // If none of these are entered, this routine complains and     //  prompts the user again.  Otherwise the operator is returned.     char getOperator()     {             System.out.println("Enter operation: + - * / q (q ==> quit) : ");             String input = scan.next();                 return getOperator();     }         int getOperand(int which)     {                 System.out.println("Enter operand" + which);                 String op = scan.next();                 return convert_from_Roman(op);     }         String convert_to_Roman(int value){             {                 String total = 0;                                 switch (value)                 {                 case 1000:                         total += 'M';                         break;                 case 500:                         total += 'D';                         break;                 case 100:                         total += 'C';                         break;                 case 50:                         total += 'L';                         break;                 case 10:                         total += 'X';                         break;                 case 5:                         total += 'V';                         break;                 case 1:                         total += 'I';                         break;                        }                                 return total;         }                             }                           int convert_from_Roman(String value)     {             String s = value.toUpperCase();             s = s.trim();             int len = s.length();             int total = 0;                         for (int i = len; i > 0; i--)             {             switch (value)                     {             case "M":                     total += 1000;                     break;             case "D":                     total += 500;                     break;             case "C":                     total += 100;                     break;             case "L":                     total += 50;                     break;             case "X":                     total += 10;                     break;             case "V":                     total += 5;                     break;             case "I":                     total += 1;                     break;                            }             }                         return total;                     }         int doArithmetic(int operand1, int operand2, char operator)     {             switch (operator)             {             case '+':                     return operand1 + operand2;             case '-':                     return operand1 - operand2;             case '*':                     return operand1 * operand2;             case '/':                     return operand1 / operand2;                                 }                             return operator;             }         public static void main(String[] args) {         RomanCalculator rc = new RomanCalculator();         while (rc.doCalculation())         {             System.out.println("Good Job");         }         System.out.println("Finished Roman Computations");     } }```
• 03-15-2013, 03:29 PM
Ronin
Re: Roman Calculator help!
Hi LetsGOBlue,

Without compiling your code I have spotted a couple of errors.

The return statement on line 33 returns a call back to the containing method. I'm assuming this should have been 'input' but this would case a mismatch between the String declaration of 'input' and the expected char return.

The for loop on line 85 in the convert_from_Roman() method starts at 'len' which is one past the last index of the array causing an expected IndexOutOfBounds error. Once this is corrected, the last character within the string will never be converted as the loop terminates before index 0.
In general this method doesn't look as though it is doing what you intended as the switch is comparing the value of 'value'. What you intended to do was to compare each character of 'value' instead of 'value' itself.

Regards.
• 03-15-2013, 04:31 PM
tamilarasi
Re: Roman Calculator help!
Quote:

Originally Posted by LetsG0Blue
Hello!

I'm having a lot of issues with my roman calculator. My first issue is that;

Code:

```    char getOperator()     {             System.out.println("Enter operation: + - * / q (q ==> quit) : ");             String input = scan.next();                 return getOperator();     }```

you have issue in your return statement like you must return string or char variable but you return the function inside the function like return getOperator(); this is not work correctly..

So you change your return statement like return input;.
and change your input variable string to char or change your return type variable char to string...

and change your code in line no 46 like String total = 0; into String total = null;

finally you check your switch case is support string variable in line 88
• 03-15-2013, 04:32 PM
LetsG0Blue
Re: Roman Calculator help!
Yeah I'm getting that outofbounds error how would I go about fixing that?

Code:

```import java.util.*; public class RomanCalculator {     // scan can now be used anywhere within this class     Scanner scan = new Scanner(System.in);         // This routine either returns false if the use wants to quit,     //    or it does one Roman Calculator calculation     boolean doCalculation()     {                                 char operand, operator = getOperator();                 if (operator == 'q')                         return false;                                 int operand1 = getOperand(1),operand2 = getOperand(2);                     int answer = doArithmetic(operand1, operand2, operator);                     String sAnswer = convert_to_Roman(answer);                     System.out.println("Answer = " + answer);                             return true;             }         // This routine prompts the user with     // Operator: +  -  *  /  q for quit     // If none of these are entered, this routine complains and     //  prompts the user again.  Otherwise the operator is returned.     char getOperator()     {             System.out.println("Enter operation: + - * / q (q ==> quit) : ");             String input = scan.next();                 return 0;                     }         int getOperand(int which)     {                 System.out.println("Enter operand" + which);                 String op = scan.next();                 return convert_from_Roman(op);     }         String convert_to_Roman(int value){             {                                 String roman = " ";                         while (value > 0 && value < 4000)                 switch (value)                 {                 case 1000:                         roman += 'M';                         break;                 case 500:                         roman += 'D';                         break;                 case 100:                         roman += 'C';                         break;                 case 50:                         roman += 'L';                         break;                 case 10:                         roman += 'X';                         break;                 case 5:                         roman += 'V';                         break;                 case 1:                         roman += 'I';                         break;                        }                                 return roman;         }                             }                           int convert_from_Roman(String value)     {             String s = value.toUpperCase();             s = s.trim();             int len = s.length();             int total = 0;                         for (int i = len; i > 0; i++)             {             char c = value.charAt(i);             switch (c)                     {             case 'M':                     total += 1000;                     break;             case 'D':                     total += 500;                     break;             case 'C':                     total += 100;                     break;             case 'L':                     total += 50;                     break;             case 'X':                     total += 10;                     break;             case 'V':                     total += 5;                     break;             case 'I':                     total += 1;                     break;                            }             }                         return total;                     }         int doArithmetic(int operand1, int operand2, char operator)     {             switch (operator)             {             case '+':                     return operand1 + operand2;             case '-':                     return operand1 - operand2;             case '*':                     return operand1 * operand2;             case '/':                     return operand1 / operand2;                                 }                             return operator;             }         public static void main(String[] args) {         RomanCalculator rc = new RomanCalculator();         while (rc.doCalculation())         {             System.out.println("Good Job");         }         System.out.println("Finished Roman Computations");     } }```
• 03-15-2013, 04:57 PM
Ronin
Re: Roman Calculator help!
Very simple.
Code:

`for (int i = len-1; i >= 0; i--) //Moves from end to start`
or
Code:

`for (int i = 0; i < len 0; i++) //Moves from start to end`
Its also worth noting that checking each character in turn would not work as intended. With the code in #4 "VI" equals 6 but "IV" also equals 6. Just something to be aware of.

Regards.
• 03-15-2013, 05:24 PM
LetsG0Blue
Re: Roman Calculator help!
Thank you! Now that's fix there's another problem.

When I enter

Quote:

Enter operation: + - * / q (q ==> quit) :
+
Enter operand1
23
Enter operand2
32
Good Job
The answer is coming out as 0 and not taking 23 + 32 and turning it into a Roman Number

Code:

```import java.util.*; public class RomanCalculator {     // scan can now be used anywhere within this class     Scanner scan = new Scanner(System.in);         // This routine either returns false if the use wants to quit,     //    or it does one Roman Calculator calculation     boolean doCalculation()     {                                 char operand, operator = getOperator();                 if (operator == 'q')                         return false;                                 int operand1 = getOperand(1),operand2 = getOperand(2);                     int answer = doArithmetic(operand1, operand2, operator);                     String sAnswer = convert_to_Roman(answer);                     System.out.println("Answer = " + answer);                             return true;             }         // This routine prompts the user with     // Operator: +  -  *  /  q for quit     // If none of these are entered, this routine complains and     //  prompts the user again.  Otherwise the operator is returned.     char getOperator()     {             System.out.println("Enter operation: + - * / q (q ==> quit) : ");             String input = scan.next();                 return 0;                     }         int getOperand(int which)     {                 System.out.println("Enter operand" + which);                 String op = scan.next();                 return convert_from_Roman(op);     }         String convert_to_Roman(int value){             {                                 String roman = " ";                         while (value > 0 && value < 4000)                 switch (value)                 {                 case 1000:                         roman += 'M';                         break;                 case 500:                         roman += 'D';                         break;                 case 100:                         roman += 'C';                         break;                 case 50:                         roman += 'L';                         break;                 case 10:                         roman += 'X';                         break;                 case 5:                         roman += 'V';                         break;                 case 1:                         roman += 'I';                         break;                        }                                 return roman;         }                             }                           int convert_from_Roman(String value)     {             String s = value.toUpperCase();             s = s.trim();             int len = s.length();             int total = 0;                         for (int i = len-1; i >= 0; i--)             {             char c = value.charAt(i);             switch (c)                     {             case 'M':                     total += 1000;                     break;             case 'D':                     total += 500;                     break;             case 'C':                     total += 100;                     break;             case 'L':                     total += 50;                     break;             case 'X':                     total += 10;                     break;             case 'V':                     total += 5;                     break;             case 'I':                     total += 1;                     break;                            }             }                         return total;                     }         int doArithmetic(int operand1, int operand2, char operator)     {             switch (operator)             {             case '+':                     return operand1 + operand2;             case '-':                     return operand1 - operand2;             case '*':                     return operand1 * operand2;             case '/':                     return operand1 / operand2;                                 }                             return operator;             }         public static void main(String[] args) {         RomanCalculator rc = new RomanCalculator();         while (rc.doCalculation())         {             System.out.println("Good Job");         }         System.out.println("Finished Roman Computations");     } }```
• 03-15-2013, 06:15 PM
Ronin
Re: Roman Calculator help!
I've managed to debug your code and there are a couple of points.

• Entering 32 and 23 won't work as your program is expecting Roman numerals.
• Your getOperator() method returns 0 instead of a valid operator. You will need to pick up a single character input from the keyboard and return this.
• The convert_to_Roman() method implements a switch statement within a while loop. Once executed, this statement will never terminate as the value of 'value' never changes within the loop.

Regards.
• 03-16-2013, 02:40 AM
LetsG0Blue
Re: Roman Calculator help!
Quote:

Originally Posted by Ronin
I've managed to debug your code and there are a couple of points.

• Entering 32 and 23 won't work as your program is expecting Roman numerals.
• Your getOperator() method returns 0 instead of a valid operator. You will need to pick up a single character input from the keyboard and return this.
• The convert_to_Roman() method implements a switch statement within a while loop. Once executed, this statement will never terminate as the value of 'value' never changes within the loop.

Regards.

Shouldn't line 18 convert the numbers to Roman?

2) How would I go about returning the a single character input?

3) What should I do to the 'value'?
• 03-16-2013, 03:29 AM
LetsG0Blue
Re: Roman Calculator help!
Quote:

Originally Posted by Ronin
I've managed to debug your code and there are a couple of points.

[*]Your getOperator() method returns 0 instead of a valid operator. You will need to pick up a single character input from the keyboard and return this.

This is the only one I can't figure out. Fixed the others.
• 03-16-2013, 04:43 AM
SJF
Re: Roman Calculator help!
Quote:

Originally Posted by LetsG0Blue
This is the only one I can't figure out. Fixed the others.

1. Read in the next line (DONE)
2. get charAt(0) (caveats here.... make sure you've got a String of at least one character length back ;) )
3. either with an if statement or switch statement make sure your character is one that you are looking for.
4. Make a choice here: IF NOT good -> requery OR return some value outside expected range
5. If good return something in expected range.... 0 = +, 1 = -, etc maybe or return '+', '-'
• 03-16-2013, 05:33 PM
jim829
Re: Roman Calculator help!
Ok. Pardon me for jumping in here at the last minute but I do have a couple of suggestions. There are some issues in the conversion methods. I would recommend that you break this up into pieces and get each piece to work independently, then integate them together.

1. Coverting to Roman numerals.

Just write a method which does this and send it a lot of test data. Make certain that numbers like 1999 convert to MCMXCIX and not MIM (the latter of which is illegal). The only valid "constructs" (in Ascii) for constructing numerals are the combinations of M, CM, D, CD, C, XC, L, XL, X, IX, V, IV, and I.

2. Converting to Decimals.

Same as above. Generate valid and invalid numerals and check for a valid decimal output. Flag errors on illegal values

3. Use the two above together to test each other.

you could put the following in a loop going from 1 to 3999 and use the loop counter as the argument.

d1 = some integer.
r1 = toRoman(d1);
d2 = toDecimal(r1).
d1 must equal d2 or you have a problem.

3. Once you have those done you can focus on the rest. Knowing that your other methods have a high probability of being correct makes it easier to debug the rest of your code.

I know you have spent a lot of time on this and am not suggesting you start over. You can just take you existing methods and apply the above.

Regards,
Jim
• 03-16-2013, 06:02 PM
Ronin
Re: Roman Calculator help!
Agreed. It is always worth testing each method to ensure they are working as expected.

Regards.