DMCA.com Protection Status Java Programming Lesson 7 Part-3 | Scanner Input (more) – Advanced Usage - ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer

Header Ads

Java Programming Lesson 7 Part-3 | Scanner Input (more) – Advanced Usage

 LESSON-7 PART-3

Scanner Input (more) – Advanced Usage

✅ Let’s continue Lesson-7 (Extended): Scanner Input – Advanced Usage
This will make you pro at taking inputs in Java 💪

In this part of Java Programming Lesson 7, we take a deeper look at the Scanner class — one of the most commonly used tools for reading user input in Java. While beginners often use Scanner only for simple nextInt() or nextLine() operations, the class actually provides powerful parsing features, input validation, and flexible data handling.

This lesson helps you master advanced input scanning, making your Java programs more interactive, reliable, and user-friendly.


📘 Scanner Input — Advanced Concepts

✅ Scanner is used to take input from keyboard
✅ Located in java.util package

import java.util.Scanner;

Create Scanner object:

Scanner sc = new Scanner(System.in);

✅ Input Types

MethodInput TypeExample
nextInt()    Integer                    25
nextDouble()    Decimal number                  99.5
nextBoolean()    Boolean (true/false)                  true
next()    Single word string                "Java"
nextLine()    Full line string        "Java Programming Course"
nextFloat()    Float                10.55f

✅ Example 1: Taking Different Inputs

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter your age: "); int age = sc.nextInt(); System.out.print("Enter your GPA: "); double gpa = sc.nextDouble(); System.out.print("Enter your name: "); sc.nextLine(); // clear buffer String name = sc.nextLine(); System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("GPA: " + gpa); } }

⚠️ Notice sc.nextLine() after nextInt()
This clears the buffer issue


✅ Example 2: Taking Multiple Words Using nextLine()

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

✅ Example 3: Check Even or Odd

System.out.print("Enter a number: "); int n = sc.nextInt(); if(n % 2 == 0) { System.out.println("Even Number"); } else { System.out.println("Odd Number"); }

✅ Example 4: User Menu Input (Switch)

System.out.println("1. Add"); System.out.println("2. Subtract"); System.out.print("Enter your choice: "); int choice = sc.nextInt();

✅ Input Validation Example

System.out.print("Enter a number: "); if(sc.hasNextInt()) { int num = sc.nextInt(); System.out.println("Valid number: " + num); } else { System.out.println("Invalid Input!"); }

🔥 BONUS — Password Input with Hidden Characters (Console)

Console console = System.console(); char[] password = console.readPassword("Enter password: "); System.out.println("Password stored securely!");

(Works when running outside IDE like Eclipse terminal)


🧠 KEY POINTS

nextLine() takes full sentence
✔ Use an extra nextLine() after numeric input
✔ Always close scanner → sc.close()
✔ Input validation using hasNextInt()


✅ Practice Task for You

Write a program to input:

  1. Full Name
  2. Mobile Number
  3. Age
  4. Course Name

Then print in a formatted way like:

Student Profile: Name: S.M. JITU HASAN Mobile: 017xxxxx Age: 21 Course: Java Programming

Conclusion

In Lesson 7 Part-3, you learned the advanced features of the Java Scanner class:

✔ Custom delimiters

✔ Reading full lines + parsing

✔ Input validation with hasNext methods

✔ Mixing nextInt() and nextLine() properly

✔ Reading until EOF

✔ Parsing complex and formatted inputs

✔ Using Scanner for files

✔ Building flexible input systems

Mastering advanced Scanner techniques makes your Java programs cleaner, smarter, and much more professional.

✅ Lesson-7 PART-3 Completed!



➡ NEXT Lesson-8


Frequently Asked Questions (FAQs)

What is advanced Scanner usage in Java?

Advanced Scanner usage involves reading multiple data types, handling whitespace, managing buffer issues, and using methods like nextLine(), next(), nextInt(), and nextDouble() correctly.

Why does Scanner skip input after reading numbers?

Scanner numeric methods leave a newline character in the input buffer. Calling nextLine() afterwards clears the buffer and prevents skipped input.

What is the difference between next() and nextLine()?

next() reads a single word (stops at space), while nextLine() reads an entire line including spaces. They behave differently with buffers.

How do I handle invalid user input with Scanner?

Use loops with try–catch blocks to validate and re-prompt when incorrect data types are entered.

How can I read integers and strings together without errors?

After nextInt() or nextDouble(), call nextLine() to clear the leftover newline before reading a full string.

What is input buffer flushing in Java?

Flushing the buffer means clearing unwanted characters (like newline) using nextLine() to avoid input mismatches.

How do I check if Scanner input is a valid number?

You can use hasNextInt(), hasNextDouble(), or wrap parsing in a try–catch to verify valid user input.

How can I prevent Scanner-related runtime errors?

Use input validation, buffer clearing, exception handling, and method selection (next vs nextLine) depending on your input requirements.

Why is Scanner commonly used in Java beginner lessons?

Scanner is simple, built-in, and supports reading all major data types, making it ideal for learning user input handling.

No comments

Thank You For Visit My Website.
I Will Contact As Soon As Possible.

Theme images by 5ugarless. Powered by Blogger.