Home java Java calculator

Java calculator

Author

Date

Category

The calculator takes an expression from the user and writes it to a string that I want to check for the presence of Roman numerals and replace them with Arabic ones. The calculator accepts numbers up to 10 as input. Only now I can not understand where the error is, it seems to me a logical error.

here is my code:

public class Main {
  private static final char exitCharacter = '!';
  public static void main (String [] args) {
    DataReader reader = new DataReader (exitCharacter);
    while (true) {
      try {
        reader.read ();
      } catch (RuntimeException e) {
        System.err.println (e.getMessage ());
        continue;
      }
      if (reader.isExitFlag ()) {
        System.out.println ("The expression contains an exit sign:" + exitCharacter);
        System.out.println ("Program termination.");
        break;
      }
      double result = Calculator.calculate (reader.getVar1 (), reader.getVar2 (), reader.getOper ());
      System.out.println (result);
    }
  }
}
import java.util.Scanner;
public class DataReader {
  private int number1;
  private int number2;
  private char operation;
  private boolean exitFlag;
  private char exitCharacter;
  private char resultChar;
  public DataReader (char exitCharacter) {
    this.exitCharacter = exitCharacter;
    this.resultChar = '=';
  }
  public void read () {
    Integer [] arabic = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    String [] roman = {"X", "IX", "VIII", "VII", "VI", "V", "IV", "III", "II", "I"};
    System.out.println ("Enter an expression consisting of two integers from 0 to 10, an operation sign and an equal sign (eg 2 + 2 =):");
    Scanner scanner = new Scanner (System.in);
    // StringBuilder builder = new StringBuilder ();
    String text = scanner.nextLine ();
    if (text.indexOf (exitCharacter)! = -1) {
      exitFlag = true;
      return;
    }
    try {
      if ((text.charAt (text.length () - 1)! = resultChar)) {
        throw new RuntimeException ();
      }
      text = text.substring (0, text.length () - 1);
      String [] blocks = text.split ("[+ - / *]");
      // check if the string blocks [0] is included in the string array of the "roman" array
      boolean flag = false;
      for (int i = 0; i & lt; roman.length; i ++) {
        if (roman [i] .equals (blocks [0]) || roman [i] .equals (blocks [1])) {
          flag = true;
        }
        // flag = true, so we will deal with Roman notation
        if (flag) {
          number1 = romanToNumber (blocks [0]);
          number2 = romanToNumber (blocks [1]);
        } else {
          number1 = Integer.parseInt (blocks [0]);
          operation = text.charAt (blocks [0] .length ());
          number2 = Integer.parseInt (blocks [1]);
        }
      }
      if ((number1 & gt; 10 || number1 & lt; 0) || (number2 & gt; 10 || number2 & lt; 0)) {
        throw new IllegalArgumentException ();
      }
    } catch (RuntimeException e) {
      throw new IllegalArgumentException ("Invalid data format");
    }
  }
  private static int romanToNumber (String roman) {
    if (roman.equals ("I")) {
      return 1;
    } else if (roman.equals ("II")) {
      return 2;
    } else if (roman.equals ("III")) {
      return 3;
    } else if (roman.equals ("IV")) {
      return 4;
    } else if (roman.equals ("V")) {
      return 5;
    } else if (roman.equals ("VI")) {
      return 6;
    } else if (roman.equals ("VII")) {
      return 7;
    } else if (roman.equals ("VIII")) {
      return 8;
    } else if (roman.equals ("IX")) {
      return 9;
    } else if (roman.equals ("X")) {
      return 10;
    } else {
      return -1;
    }
  }
  public int getVar1 () {
    return number1;
  } 
Public int getvar2 () {
    RETURN NUMBER2;
  }
  Public Char Getoper () {
    RETURN OPERATION;
  }
  Public Boolean IsExitflag () {
    Return exitflag;
  }
}
Public Class Calculator {
  Private Calculator () {}
  Public Static Double Calculate (INT NUMBER1, INT NUMBER2, CHAR OPERATION) {
    INT RESULT = 0;
    Switch (Operation) {
      Case '+': Result = Number1 + Number2; Break;
      Case '-': Result = Number1 - NUMBER2; Break;
      Case '*': Result = Number1 * Number2; Break;
      Case '/': Result = Number1 / Number2; Break;
      DEFAULT: Throw New IllegAlarGumexception ("Not the correct operation sign");
    }
    RETURN RESULT;
  }
}

Release Operation = Text.Charat (Blocks [0] .length ()); For the Forcycle cycle, the calculator refuses to work with Roman numerals and throws out the exception

2 + ii =
Invalid data format

Answer 1, Authority 100%

if (Roman [i] .equals (Blocks [0]) || Roman [i] .equals (Blocks [1])){
        Flag = True;
      }
      // Flag = True, then we will deal with Roman designations
      if (flag) {
        NUMBER1 = ROMANTONUMBER (BLOCKS [0]);
        NUMBER2 = ROMANTONUMBER (BLOCKS [1]);
      } else {
        Number1 = integer.paraseint (blocks [0]);
        Operation = Text.Charat (Blocks [0] .length ());
        Number2 = integer.paraseint (blocks [1]);
      }

Incorrect logic. You set the flag after checking on the "X" and you start to parse the operands as a decimal. If the operands – Roman, but not "X" , an error occurs

.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions