Beginner’s Guide to Creating a Calculator in Java Using Switch Statements

By Vandu
May 2, 2025

Follow us on


Learn how to build a simple calculator in Java using switch statements with easy step-by-step instructions, source code, and clear explanations for beginners.

Beginner’s Guide to Creating a Calculator in Java Using Switch Statements

Build a Simple Calculator Using Switch Statements in Java

Creating a calculator is one of the best ways to start learning programming. In this article, we will create a simple calculator in Java using switch statements. This calculator will perform basic arithmetic operations like addition, subtraction, multiplication, and division based on user input.


📌 What is a Switch Statement in Java?

A switch statement in Java is used to perform different actions based on different conditions. It works like an if-else-if ladder but makes the code cleaner and more readable when you have multiple conditions to check.


🛠️ Features of Our Calculator:

  • Takes two numbers as input from the user.

  • Takes an operator as input (+, -, *, /).

  • Performs the chosen operation using a switch statement.

  • Displays the result to the user.


Java Code for Simple Calculator Using Switch

import java.util.Scanner;

public class SimpleCalculator {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Step 1: Take first number input
        System.out.print("Enter the first number: ");
        double num1 = scanner.nextDouble();

        // Step 2: Take second number input
        System.out.print("Enter the second number: ");
        double num2 = scanner.nextDouble();

        // Step 3: Ask the user for the operation
        System.out.print("Choose an operation (+, -, *, /): ");
        char operator = scanner.next().charAt(0);

        double result;

        // Step 4: Use switch statement to perform calculation
        switch (operator) {
            case '+':
                result = num1 + num2;
                System.out.println("Result: " + result);
                break;
            case '-':
                result = num1 - num2;
                System.out.println("Result: " + result);
                break;
            case '*':
                result = num1 * num2;
                System.out.println("Result: " + result);
                break;
            case '/':
                if (num2 != 0) {
                    result = num1 / num2;
                    System.out.println("Result: " + result);
                } else {
                    System.out.println("Error: Cannot divide by zero!");
                }
                break;
            default:
                System.out.println("Invalid operator! Please use +, -, *, or /.");
        }

        scanner.close();
    }
}

🧠 Step-by-Step Explanation of the Code

🔹 Step 1: Import Scanner for User Input

import java.util.Scanner;

We import Scanner class from the Java utility package so that we can take input from the user.


🔹 Step 2: Get the Numbers

double num1 = scanner.nextDouble();
double num2 = scanner.nextDouble();

We ask the user to enter two numbers. These are stored as double to allow decimal values.


🔹 Step 3: Get the Operator

char operator = scanner.next().charAt(0);

We ask the user to enter an operator like +, -, *, or /. We read the first character of the input using .charAt(0).


🔹 Step 4: Use Switch Statement

switch (operator) {
    case '+':
        ...
    case '-':
        ...
    // other cases
}

The switch checks the value of operator and runs the matching case block.


🔹 Step 5: Perform Operation & Display Result

For each case, we perform the calculation and print the result. If the operator is invalid, the default case handles it with a message.


❗ Important Notes:

  • Always check for division by zero to avoid errors.

  • switch is case-sensitive, so only valid symbols will work.

  • Use scanner.close() at the end to prevent resource leaks.


🎯 Conclusion

You’ve now learned how to create a basic calculator in Java using switch statements. This is a great beginner project to understand user input, control statements, and basic arithmetic operations.

Once you’re comfortable with this, try expanding the calculator by:

  • Adding more operations like modulus (%)

  • Supporting multiple calculations in a loop

  • Handling invalid input more gracefully


© 2025 Pay18News. All rights reserved.