Day 4 Java Tutorial: Scanner Input + Build a Basic Calculator

Day 4 - Scanner Input + Build a Basic Calculator

Hello everyone, welcome back to our 100-day Java course.
Yesterday we learned about variables and data types, which teaches the computer how to remember information.

Here is the problem.
Right now, our programs only work with values we type directly into the code. That means if you write a program asking for someone’s age, you can only test it with the one age you coded.

That is like making an ATM that only works with your PIN. Pretty useless, right?

Today we will make our programs interact with the user. The tool we will use is called Scanner.

What is Scanner

Think of Scanner like a microphone for your program. It lets your program listen to the user.

How it works:

  1. You ask a question
  2. The user types something
  3. Scanner reads it
  4. Your program uses the input

How to Use Scanner

Step 1: Import Scanner

import java.util.Scanner;

Step 2: Create a Scanner object

Scanner sc = new Scanner(System.in);

Step 3: Ask the user for input

System.out.println("Enter your name: ");
String name = sc.nextLine();

Step 4: Print it back

System.out.println("Hello, " + name);

Your program now interacts with the user like a simple chatbot.

Types of Input with Scanner

  • nextLine() for text like words or sentences
  • nextInt() for integers
  • nextDouble() for decimals
  • nextBoolean() for true or false

Example

int age = sc.nextInt();
double salary = sc.nextDouble();

Real Example: Making a Basic Calculator

Here is a simple calculator using Scanner.

import java.util.Scanner;

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

        System.out.println("Welcome to Java Calculator");
        System.out.print("Enter first number: ");
        double num1 = sc.nextDouble();

        System.out.print("Enter second number: ");
        double num2 = sc.nextDouble();

        System.out.println("Choose operation (+, -, *, /): ");
        char operator = sc.next().charAt(0);

        double result = 0;

        if (operator == '+') {
            result = num1 + num2;
        } else if (operator == '-') {
            result = num1 - num2;
        } else if (operator == '*') {
            result = num1 * num2;
        } else if (operator == '/') {
            if (num2 != 0) {
                result = num1 / num2;
            } else {
                System.out.println("Error: Cannot divide by zero");
                return;
            }
        } else {
            System.out.println("Invalid operator");
            return;
        }

        System.out.println("Result: " + result);
    }
}

How it works

  1. Program asks for two numbers
  2. You choose an operation (+, -, *, /)
  3. Program calculates and prints the result

Example run

Welcome to Java Calculator
Enter first number: 20
Enter second number: 5
Choose operation (+, -, *, /): *
Result: 100.0

Scanner is Like Jarvis

Think of Scanner like Jarvis’s voice recognition.
Tony Stark says, “Jarvis, set power to 50 percent.”
Jarvis takes the input, understands it, and changes the suit’s settings.

That is exactly what Scanner does for our Java program.

Wrap Up

Today we learned

  • How to take input from users using Scanner
  • Different methods like nextLine, nextInt, nextDouble
  • Built a working calculator app in Java

Tomorrow we will talk about operators in Java, teaching the computer how to do math and logic more efficiently.

Reference:

You can also check out:

About Us

If the information you are looking for is not available here, please contact us. Additionally, follow us on our social media platforms for updates and more information.

Leave a Comment

Your email address will not be published. Required fields are marked *