Java User Input In Hindi

📔 : Java 🔗

Java programming language में आप user input data को भी handle कर सकते हैं , user द्वारा enter किये गए data को handle करने के लिए java.util package की Scanner class का use किया जाता है।

Java User Input Example

File : UserInput.java

CopyFullscreenClose FullscreenRun
// import the Scanner class from util package.
import java.util.Scanner; 

public class UserInput {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String name;
    
    // Enter your name and press Enter.
    System.out.println("Enter name : "); 
    name = scan.nextLine();   
       
    System.out.println("Your name is : " + name);        
  }
}
Output
Enter name : Rahul
Your name is : Rahul

ऊपर दिए गए example में हमने , nextLine() method का use किया है जो input की गयी string line को read करता है। अलग अलग type के according scanner class में अलग - अलग methods हैं -

Java Scanner class methods
MethodDescription
nextBoolean()to read a boolean value from input.
nextByte()to read a byte value from input.
nextDouble()to read a double value from input.
nextFloat()to read a float value from input.
nextInt()to read a int value from input.
nextLine()to read a String value from input.
nextLong()to read a long value from input.
nextShort()to read a short value from input.

अब एक example देखते हैं जिसमे हम user से अलग अलग type का input handle करेंगे -

File : Student.java

CopyFullscreenClose FullscreenRun
import java.util.Scanner;

public class Student {
  public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    
    // String input.
    System.out.println("Student name :");
    String name = scan.nextLine();
    
    // Integer input.
    System.out.println("Student age :");
    int age = scan.nextInt();
    
    // double input.
    System.out.println("Student salery :");
    double salary = scan.nextDouble();

    // Output.
    System.out.println("Name : " + name);
    System.out.println("Age : " + age);
    System.out.println("Salary : " + salary);
  }
}
Output
Student name :Ravi
Student age :
23
Student salery :
67.56
Name : Ravi
Age : 23
Salary : 67.56l

ध्यान रहे , जिस type की value के लिए method use कर रहे हैं input की जाने वाली value भी उसी type की हो otherwise InputMismatchException exception आएगी।

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 4.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook