User Input in Java using Scanner (Complete Guide with Examples)

 


Taking input from users is one of the most important parts of any application. Whether it's a login form, calculator, or real-time system—user input is everywhere.

In Java, the most common way to take input is using the Scanner class.


🔹 What is Scanner in Java?

Scanner is a class in Java used to read input from different sources, such as:

  • Keyboard (System.in)
  • Files
  • Strings

👉 It belongs to the package:

java.util

🔹 Why Use Scanner?

Before Scanner, Java input handling was complex.

👉 Scanner makes it:

  • Simple
  • Readable
  • Flexible

🔹 Step 1: Import Scanner Class

Before using Scanner, you must import it.

import java.util.Scanner;

🔹 Step 2: Create Scanner Object

Scanner sc = new Scanner(System.in);

👉 Here:

  • sc → Object name
  • System.in → Standard input (keyboard)

🔹 Step 3: Take Input from User

🔸 Example (Integer Input)

import java.util.Scanner;

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

System.out.print("Enter a number: ");
int num = sc.nextInt();

System.out.println("You entered: " + num);
}
}

🔹 Common Scanner Methods

MethodDescription
nextInt()Reads integer
nextDouble()Reads double
nextFloat()Reads float
next()Reads single word
nextLine()Reads full line
nextBoolean()Reads boolean

🔹 Example: Multiple Inputs

import java.util.Scanner;

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

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

System.out.print("Enter your age: ");
int age = sc.nextInt();

System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
🚀 Kickstart your career with Ashok IT’s Full Stack Java Program—designed with real-time
training and dedicated placement assistance to help you land your first IT job.

🔹 Difference Between next() and nextLine()

👉 Very important for beginners

🔸 next()

  • Reads only one word
  • Stops at space

🔸 nextLine()

  • Reads full sentence (including spaces)

🔸 Example:

String name1 = sc.next(); // John
String name2 = sc.nextLine(); // John Doe

🔹 Common Problem (Very Important)

❌ Issue: nextLine() skipped after nextInt()

int age = sc.nextInt();
String name = sc.nextLine(); // skipped

👉 Reason:

  • nextInt() leaves newline in buffer

✅ Solution:

int age = sc.nextInt();
sc.nextLine(); // consume leftover newline
String name = sc.nextLine();

🔹 Real Example: Simple Calculator

import java.util.Scanner;

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

System.out.print("Enter first number: ");
int a = sc.nextInt();

System.out.print("Enter second number: ");
int b = sc.nextInt();

int sum = a + b;

System.out.println("Sum: " + sum);
}
}

👉 Demonstrates:

  • User input
  • Arithmetic operation

🔹 Closing Scanner (Best Practice)

Always close Scanner after use:

sc.close();

👉 Prevents memory/resource leaks.


🔹 Common Errors & Fixes

❌ InputMismatchException

👉 Enter correct data type


❌ nextLine() skipping issue

👉 Use extra sc.nextLine()


❌ Forgetting import

👉 Add:

import java.util.Scanner;

🔹 Best Practices

✔ Always validate user input
✔ Use appropriate Scanner methods
✔ Close Scanner after use
✔ Handle exceptions for safety

🔥 Don’t just learn Java—build a career! Join Ashok IT’s Full Stack Java training with complete placement support and real-time project experience.


🔹 Where Scanner is Used

  • Console-based applications
  • Testing programs
  • Learning and practice
  • Small tools & utilities

👉 For advanced apps, frameworks handle input differently.


🔹 What to Learn Next

After Scanner, move to:

  • Control Statements (if, switch)
  • Loops (for, while)
  • Arrays

👉 This helps you build real programs.


🔹 Conclusion

Scanner is a simple yet powerful tool for taking user input in Java.

👉 Once you master it:

  • You can build interactive programs
  • Handle real-world data
  • Move towards advanced development

Remember:
No input → No interaction → No real application

Comments