🟢 Foundation · Lesson 09
User Input using Scanner
User Input using Scanner
Reading Input with Scanner
The Scanner class reads user input from the keyboard. Import it from
java.util.Example Program
import java.util.Scanner;
public class InputDemo {
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("Hello " + name + ", age " + age);
sc.close();
}
}Enter your name: Aman
Enter your age: 18
Hello Aman, age 18
Common Scanner Methods
| Method | Reads |
|---|---|
| nextInt() | an integer |
| nextDouble() | a decimal |
| nextLine() | a full line of text |
| next() | a single word |
Summary
- Import
java.util.Scannerand create a Scanner onSystem.in. - Use nextInt(), nextDouble(), nextLine() to read input.
Scanner से Input पढ़ना
Scanner class keyboard से user input पढ़ती है। इसे
java.util से import करें।Example Program
import java.util.Scanner;
public class InputDemo {
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("Hello " + name + ", age " + age);
sc.close();
}
}Enter your name: Aman
Enter your age: 18
Hello Aman, age 18
Common Scanner Methods
| Method | पढ़ता है |
|---|---|
| nextInt() | integer |
| nextDouble() | decimal |
| nextLine() | पूरी line |
| next() | एक word |
सारांश
java.util.Scannerimport करकेSystem.inपर Scanner बनाएं।- nextInt(), nextDouble(), nextLine() से input पढ़ें।