🟢 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

MethodReads
nextInt()an integer
nextDouble()a decimal
nextLine()a full line of text
next()a single word

Summary

  • Import java.util.Scanner and create a Scanner on System.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.Scanner import करके System.in पर Scanner बनाएं।
  • nextInt(), nextDouble(), nextLine() से input पढ़ें।
← Back to Java Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

\n