Results 1 to 12 of 12
  1. #1
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Roman Calculator help!

    Hello!

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

    Java 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.

    Java 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");
        }
    
    }

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    348
    Rep Power
    4

    Default 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.

  3. #3
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Roman Calculator help!

    Quote Originally Posted by LetsG0Blue View Post
    Hello!

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

    Java 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
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  4. #4
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Roman Calculator help!

    Yeah I'm getting that outofbounds error how would I go about fixing that?

    Java 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");
        }
    
    }

  5. #5
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    348
    Rep Power
    4

    Default Re: Roman Calculator help!

    Very simple.
    Line 88 should read:
    Java Code:
    for (int i = len-1; i >= 0; i--) //Moves from end to start
    or
    Java 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.

  6. #6
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Roman Calculator help!

    Thank you! Now that's fix there's another problem.

    When I enter

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

    Java 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");
        }
    
    }

  7. #7
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    348
    Rep Power
    4

    Default 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.

  8. #8
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Roman Calculator help!

    Quote Originally Posted by Ronin View Post
    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'?

  9. #9
    LetsG0Blue is offline Member
    Join Date
    Feb 2013
    Posts
    68
    Rep Power
    0

    Default Re: Roman Calculator help!

    Quote Originally Posted by Ronin View Post
    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.

  10. #10
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: Roman Calculator help!

    Quote Originally Posted by LetsG0Blue View Post
    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 '+', '-'

  11. #11
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,650
    Rep Power
    5

    Default 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
    Last edited by jim829; 03-16-2013 at 04:40 PM. Reason: typo
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  12. #12
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    348
    Rep Power
    4

    Default Re: Roman Calculator help!

    Agreed. It is always worth testing each method to ensure they are working as expected.

    Regards.

Similar Threads

  1. Roman Numerals help
    By umaza in forum New To Java
    Replies: 2
    Last Post: 05-22-2012, 01:02 AM
  2. How to identify a roman numeral
    By dunworry in forum New To Java
    Replies: 13
    Last Post: 12-19-2010, 08:13 AM
  3. Replies: 11
    Last Post: 11-02-2010, 05:46 PM
  4. Convert roman numerals
    By matzahboy in forum New To Java
    Replies: 4
    Last Post: 02-21-2010, 10:06 PM
  5. Roman numeral to Decimal
    By hector100 in forum Advanced Java
    Replies: 3
    Last Post: 04-04-2009, 07:48 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •