Java calculator code

How to make a Light Java Calculator

How to Make a light Java Calculator within few minutes!

Which won’t crash when a user enters a string instead of an Integer of course. So let us get started! Here is the source code of that java calculator. I tried to make it as simple as possible.

 

Everything is explained using comments and you are free to use the source code 😀

Java Calculator Code

import java.util.Scanner;

public class Main {
    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {

        int number1 = 0;
        int number2 = 0;

        System.out.println("1) Enter add to do addition" +
                " \n2) Enter sub to do Subtraction" +
                " \n3) Enter div to do division " +
                " \n4) Enter mult to do Multiplication");

        String Choice = scanner.next();
        
        // Converting Choice string to Lowercase 
        Choice = Choice.toLowerCase();
        
        if (Choice.equals("add")) {

            // Assigning value to number 1 and number 2 by calling    //methods number1 and number2 .
            number1 = number1();
            number2 = number2();
            // Adding number 1 and number 2.
            int Answer = number1 + number2;
            System.out.println(Answer);
        } else if (Choice.equals("sub")) {
            // Assigning value to number 1 and number 2 by calling //methods number1 and number2 .
            number1 = number1();
            number2 = number2();
            // Subtracting number 1 and number 2.
            int Answer = number1 - number2;
            System.out.println(Answer);
        } else if (Choice.equals("div")) {
            // Assigning value to number 1 and number 2 by calling //methods number1 and number2 .
            number1 = number1();
            number2 = number2();
            // Dividing number 1 and number 2.
            int Answer = number1 / number2;
            System.out.println(Answer);
        } else if (Choice.equals("mult")) {
            // Assigning value to number 1 and number 2 by calling //methods number1 and number2 .
            number1 = number1();
            number2 = number2();
            // Multiplying number 1 and number 2.
            int Answer = number1 * number2;
            System.out.println(Answer);
        } else {
            System.out.println(" Invalid choice");

        }
    }
    // Method named number1 
    private static int number1() {
        
        // Try and catch block to catch errors and exceptions
        try {
            int number1;
            System.out.println(" Enter first number");

            number1 = scanner.nextInt();

            return number1;
        } catch (Exception e) {
            System.out.println(" You are only allowed to enter integers ");
            // Calling the method again in case of an error 
            number1();
        }

        return 0;
    }
    // Method named number2 
    private static int number2() {
        // Try and catch block to catch errors and exceptions
        try {
            int number2;
            System.out.println(" Enter first number");

            number2 = scanner.nextInt();

            return number2;
        } catch (Exception e) {
            System.out.println(" You are only allowed to enter integers ");
            // Calling the method again in case of an error 
            number2();
        }
        return 0;
    }
}

 

The calculator creation is done now. So, don’t forget to comment and share this post with your friends if you liked it!

Leave a Reply