Info-ScoopInfo-Scoop
Home
Articles
FLL Robotics
SponsorsOur Achievements
Facts
Space FactsTechnology FactsAnimal FactsShort ReadsFood FactsGeography FactsLanguage Facts
Hobbies
Raspberry PiSketches
Coding
Python ProjectsJava Projects
About
Quizzes
Books
Tongue Twisters
Sign In
This is a new site! Contact us if there are bugs

Contact Us

mail.infoscoop@gmail.com

© Info-Scoop 2026
← Back to Java Projects

Java Calculator

About This Project

This Java Calculator is a beginner-friendly project that demonstrates how to build a functional calculator using Java. It supports basic arithmetic operations and provides a simple command-line interface using the Scanner class.

Features

  • Basic arithmetic operations: Addition, Subtraction, Multiplication, Division
  • Looping menu for multiple calculations
  • Error handling for division by zero
  • Uses Scanner to read user input
  • Easy to understand and modify code

Source Code

import java.util.Scanner;

public class Calculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Welcome to the Java Calculator!");
        System.out.println("Select an operation:");
        System.out.println("1. Add");
        System.out.println("2. Subtract");
        System.out.println("3. Multiply");
        System.out.println("4. Divide");

        while (true) {
            System.out.print("Enter your choice (1/2/3/4): ");
            int choice = scanner.nextInt();

            if (choice >= 1 && choice <= 4) {
                System.out.print("Enter first number: ");
                double num1 = scanner.nextDouble();
                System.out.print("Enter second number: ");
                double num2 = scanner.nextDouble();

                switch (choice) {
                    case 1:
                        System.out.println("Result: " + num1 + " + " + num2 + " = " + (num1 + num2));
                        break;
                    case 2:
                        System.out.println("Result: " + num1 + " - " + num2 + " = " + (num1 - num2));
                        break;
                    case 3:
                        System.out.println("Result: " + num1 + " * " + num2 + " = " + (num1 * num2));
                        break;
                    case 4:
                        if (num2 != 0) {
                            System.out.println("Result: " + num1 + " / " + num2 + " = " + (num1 / num2));
                        } else {
                            System.out.println("Error: Division by zero");
                        }
                        break;
                }
            } else {
                System.out.println("Invalid input");
            }

            System.out.print("Do another calculation? (yes/no): ");
            String again = scanner.next();
            if (!again.equalsIgnoreCase("yes")) {
                break;
            }
        }
        scanner.close();
    }
}

How It Works

  1. The program creates a Scanner to read input from the user
  2. It displays a menu with four operation choices
  3. User selects an operation by entering a number (1-4)
  4. User enters two numbers for the calculation
  5. A switch statement performs the selected operation
  6. Result is displayed to the user, then the loop asks if they want to continue

Concepts Used

Scanner Input
While Loops
Switch Statements
Conditionals