DMCA.com Protection Status Java Programming Lesson-10 | Advanced Scanner Input & Parsing / Reading Strings & Numbers - ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer ZAREEN TECH WAVE -Largest Tutorials Website For Freelancer

Header Ads

Java Programming Lesson-10 | Advanced Scanner Input & Parsing / Reading Strings & Numbers

Java Programming Lesson-10


Welcome to Lesson-10: Advanced Scanner Input & Parsing — this is essential for real-world programs where user input isn’t always simple.


✅ Scanner Class Recap

Scanner is used to take input from the user.

import java.util.Scanner; Scanner sc = new Scanner(System.in);

Methods:

MethodReads
nextInt()           Integer
nextFloat()           Float
nextDouble()           Double
next()           Single word String
nextLine()           Full line String

✅ 1️⃣ Reading Numbers & Strings Together

Problem: Mixing nextInt() and nextLine() can skip input.

Scanner sc = new Scanner(System.in); System.out.print("Enter age: "); int age = sc.nextInt(); // reads int sc.nextLine(); // consume leftover newline System.out.print("Enter name: "); String name = sc.nextLine(); // reads full line

✅ 2️⃣ Parsing Strings to Numbers

Sometimes input comes as String, we need to convert.

String str = "123"; int num = Integer.parseInt(str); // String → int double d = Double.parseDouble("12.34"); // String → double
Example:
Scanner sc = new Scanner(System.in); System.out.print("Enter a number as text: "); String input = sc.nextLine(); int number = Integer.parseInt(input); System.out.println("Number + 10 = " + (number + 10));

✅ 3️⃣ Validating Input Using Try-Catch

Scanner sc = new Scanner(System.in); System.out.print("Enter an integer: "); try { int num = sc.nextInt(); System.out.println("You entered: " + num); } catch(Exception e) { System.out.println("Invalid input!"); }

✅ 4️⃣ Reading Multiple Values in One Line

Scanner sc = new Scanner(System.in); System.out.println("Enter 3 numbers separated by space:"); int a = sc.nextInt(); int b = sc.nextInt(); int c = sc.nextInt(); System.out.println("Sum = " + (a+b+c));

✅ 5️⃣ Parsing Complex Input

Example: User enters "25,30,45" → split and parse

Scanner sc = new Scanner(System.in); System.out.print("Enter numbers separated by comma: "); String input = sc.nextLine(); String parts[] = input.split(","); int sum = 0; for(String p : parts){ sum += Integer.parseInt(p.trim()); } System.out.println("Sum = " + sum);

✅ Output (input: 10, 20, 30):

Sum = 60

📝 Exercise for You ✅

Create a program that:

1️⃣ Takes full name as input
2️⃣ Takes age as input
3️⃣ Takes comma-separated numbers → calculates sum
4️⃣ Handles invalid number input with try-catch

Example Output:

Enter full name: Jitu Hasan Enter age: 21 Enter numbers separated by comma: 10, 20, 30 Hello Jitu Hasan, Age: 21 Sum of numbers: 60

Reply with:
✅ Your Code
✅ Output

I’ll check & improve it 🔥


🎯 Quick Quiz

1️⃣ Which Scanner method reads whole line?
A) next()
B) nextLine()
C) nextInt()

2️⃣ Convert "45" (String) to int?
A) Integer.parse("45")
B) Integer.parseInt("45")
C) Integer.toInt("45")

Reply like:
1 → ?, 2 → ?


✅ Lesson-10 Completed!





✅ NEXT PART - Lesson-11: Object-Oriented Programming (Classes & Objects).

No comments

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

Theme images by 5ugarless. Powered by Blogger.