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.
- An equation cannot begin with an operator (
+,/, and so on); - An equation cannot end with an operator;
- An operator cannot directly follow another operator (for example,
2+-3); - Division by zero is forbidden.
The cases described above should be handled in the following way:
- No character should be added to the
EquationLabelif users try to enter an operator as the first character; - Provide visual feedback to users if the equation ends with an operator. Change the font color of the
EquationLabelwith the help ofColors.RED.darker()after theEqualsbutton has been clicked; - If two operators are inserted consecutively, then the second operator should replace the first;
- Similar to the second case, once the
Equalsbutton has been clicked, change the color of theEquationLabelif the equation contains division by zero.
Objectives
In this stage, you should:
- Properly format and validate input expressions;
- Maintain the names of the interface components (
JLabelsandJButtons) from the previous stage.
Examples
Here is an example of how your calculator can look like: