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 💪


📘 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

✅ Lesson-7 PART-3 Completed!



➡ NEXT Lesson-8

No comments

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

Theme images by 5ugarless. Powered by Blogger.