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:
- You ask a question
- The user types something
- Scanner reads it
- 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 sentencesnextInt()for integersnextDouble()for decimalsnextBoolean()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
- Program asks for two numbers
- You choose an operation (+, -, *, /)
- 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:
- Learn Java Basics (Day 1) – Install JDK and Run Your First Program
- Learn Java Variables and Data Types (Day 2): Java for Beginners
- Day 3: Java Variables and Data Types Explained with Real-Life Examples
- Beginner’s Guide to Web Development
About Us
- Akhil Boddu’s YouTube Channel – Follow my vlogs for personal experiences and tips.
- Code With Me YouTube Channel – Explore tech tutorials and coding advice.
- WeblogTrips LinkedIn – Connect with me on LinkedIn for more insights and discussions.
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.







