If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
Java programming language में आप user input data को भी handle कर सकते हैं , user द्वारा enter किये गए data को handle करने के लिए java.util package की Scanner class का use किया जाता है।
File : UserInput.java
// 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);
}
}
Enter name : Rahul Your name is : Rahul
ऊपर दिए गए example में हमने , nextLine() method का use किया है जो input की गयी string line को read करता है। अलग अलग type के according scanner class में अलग - अलग methods हैं -
Method | Description |
---|---|
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
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);
}
}
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 आएगी।
Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)
Get connected with me. :) LinkedIn Twitter Instagram Facebook