Desktop Calculator. Stage 4/5

Handling errors

Report a typo

Description

Every interactive piece of software must have some form of input validation and error handling to maintain a proper user experience and prevent crashes. In this stage, we are going to handle errors and validate user input and equations.

There's one thing in our program that can hamper its execution. It is a user-provided equation. Let's establish a set of guidelines for our program.

  1. An equation cannot begin with an operator (+, /, and so on);
  2. An equation cannot end with an operator;
  3. An operator cannot directly follow another operator (for example, 2+-3);
  4. Division by zero is forbidden.

The cases described above should be handled in the following way:

  1. No character should be added to the EquationLabel if users try to enter an operator as the first character;
  2. Provide visual feedback to users if the equation ends with an operator. Change the font color of the EquationLabel with the help of Colors.RED.darker() after the Equals button has been clicked;
  3. If two operators are inserted consecutively, then the second operator should replace the first;
  4. Similar to the second case, once the Equals button has been clicked, change the color of the EquationLabel if the equation contains division by zero.

Objectives

In this stage, you should:

  1. Properly format and validate input expressions;
  2. Maintain the names of the interface components (JLabels and JButtons) from the previous stage.

Examples

Here is an example of how your calculator can look like:

In the calculator, incorrect equation input is highlighted in red

Write a program
IDE integration
Checking the IDE status
___

Create a free account to access the full topic