0

Sorry if this is a simple error, I am still very new to Java and have trouble with all the rules. I am trying to create a calculator that takes in roman numerals, converts them to integers, them performs operations with them, and then converts them back roman numerals to print by calling methods. getOperand It asks me for twice (e.g Console Output:please choose an operator: +, - , * , or / * Enter operand: Enter operand: xx 0 do you want to continue? y/n as in the program is reading my input as 200=0 not 1010.) and I assume it allows me two inputs but I am not sure how to input two separate values. Any suggestions? Here is my code, everything else should be working but let me know if there are any dumb mistakes:


import java.util.Scanner;

public class RomanCalculator {
    public static Scanner kbInput = new Scanner(System.in);

    public static String doMath(char operator, int firstNum, int secondNum) {
        switch (operator) {
        case '+':
            return convertToRoman(firstNum + secondNum);
        case '-':
            return convertToRoman(firstNum - secondNum);
        case '*':
            return convertToRoman(firstNum * secondNum);
        case '/':
            return convertToRoman(firstNum / secondNum);

        }

        return "omething";

        /*
         * This method will perform the arithmetic indicated by the operator (+ -* /),
         * invoke convertToRoman to convert answer to Roman number, then return answer
         */
    }

    public static char getOperator() {
        System.out.println("please choose an operator: +, - , * , or /");
        return kbInput.next().charAt(0);
    }

    public static int getOperands() {
        while (true) {
            System.out.print("Enter operand" +  ": ");
            String roman = kbInput.nextLine().toUpperCase();
            int romanNum = convertFromRoman(roman);
            if (romanNum >= 0)
             return romanNum;
            else
             System.out.println("Bad operand, please try again");
        
        }
        /*This routine should prompt the user to enter Roman number. 
      convertFromRoman needs to be invoked to convert the Roman number to an integer.
      If the input is invalid (-1 returned from convertFromRoman)
      then complain and prompt the user again. 
    */ 
    }
    public static int convertFromRoman(String roman) {
        
               int result = 0;

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

                int s1 = num(roman.charAt(i));

                if (s1 < 0) {
                 System.out.println("Invalid operand");
                 
                }
                if (i + 1 < roman.length()) {
                 int s2 = num(roman.charAt(i + 1));
                 if (s2 < 0) {
                  System.out.println("Invalid operand");
                  
                 }

                 if (s1 >= s2) {
                  result = result + s1;
                 } else {
                  result = result + s2 - s1;
                  i++;
                 }
                } else {
                 result = result + s1;
                 i++;
                }
               }

               return result;
            
            }
        /*
         * This method will convert Roman number to integer
         * return -1 when input is invalid
         * 
         * */
    

    public static String convertToRoman(int num) {
        System.out.println(num);
        String roman = "";
        while (num > 0) {
            while (num >= 1000) {
                roman = roman + "M";
                num -= 1000;
            }
            while (num >= 500) {
                roman = roman + "M";
                num -= 500;
            }
            while (num >= 100) {
                roman = roman + "D";
                num -= 100;
            }
            while (num >= 50) {
                roman = roman + "C";
                num -= 50;
            }
            while (num >= 10) {
                roman = roman + "X";
                num -= 10;
            }
            while (num >= 5) {
                roman = roman + "V";
                num -= 5;
            }
            while (num >= 1) {
                roman = roman + "I";
                num -= 1;
            }
        }
        return roman;
    }

    static int num(char r) {
        if (r == 'I')
            return 1;
        if (r == 'V')
            return 5;
        if (r == 'X')
            return 10;
        if (r == 'L')
            return 50;
        if (r == 'C')
            return 100;
        if (r == 'D')
            return 500;
        if (r == 'M')
            return 1000;
        return -1;

        /*
         * This method will convert integer to Roman number
         */
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String result;
        do {

            result = doMath(getOperator(), getOperands(), getOperands());

            System.out.println(result);
            System.out.println("do you want to continue? y/n");
            kbInput.nextLine();
        } while (kbInput.nextLine().charAt(0) == 'y');
        System.out.println("Have a nice day!");
    }

}```
RacerX142
  • 17
  • 2

0 Answers0