This project is a simple Calculator developed in Java using JOptionPane (Swing) for graphical user interaction. The program allows the user to perform basic arithmetic operations between two numbers.
The calculator supports addition, subtraction, multiplication, and division, and includes error handling for invalid inputs.
-
Graphical interface using dialog boxes
-
Supports basic operations:
- Addition
- Subtraction
- Multiplication
- Division
-
Input validation
-
Error handling for non-numeric inputs
-
Prevents invalid menu options
- Java
- Java Swing (JOptionPane)
-
The program displays a menu with the available operations:
- 1 → Add
- 2 → Subtract
- 3 → Multiplication
- 4 → Division
-
The user selects an operation.
-
The user enters two integer numbers.
-
The program performs the selected operation.
-
The result is displayed in a dialog box.
-
If the user enters invalid data, the program displays an error message and restarts.
| Option | Operation | Formula |
|---|---|---|
| 1 | Addition | a + b |
| 2 | Subtraction | a - b |
| 3 | Multiplication | a * b |
| 4 | Division | a / b |
-
Make sure Java JDK is installed.
-
Compile the program:
javac Calculator.java -
Run the program:
java Calculator
The program handles the following errors:
- Invalid menu option (must be between 1 and 4)
- Non-numeric input (NumberFormatException)
- Input mismatch errors
If an error occurs, the program shows an error message and restarts automatically.
This project is useful for practicing:
- Conditional statements
- Exception handling
- User input using Swing
- Basic arithmetic operations in Java
Developed as a basic Java practice project to practice programming logic, exception handling, and GUI interaction using Java Swing.