- How to Take Input From User in Java?
- Ways to Take Input from User in Java
- The Scanner Class
- Syntax
- Scanner Class Methods
- Example
- Output
- Explanation
- Advantages of Scanner class
- Disadvantages of Scanner class
- The BufferedReader class
- Syntax
- Example
- Output
- Explanation
- Advantages of BufferedReader class
- Disadvantages of BufferedReader class
- The Console class
- Syntax
- Example
- Output
- Explanation
- Advantages of Console class
- Disadvantages of BufferedReader class
- Java User Input (Scanner)
- Example
- Input Types
- Example
- Java input value user
- Learn Latest Tutorials
- Preparation
- Trending Technologies
- B.Tech / MCA
- Javatpoint Services
- Training For College Campus
How to Take Input From User in Java?
Input and output are two of the main essential aspects of any programming language. The keyboard and Screen are the basic devices for taking input and output respectively. User input is significant in making the application interactive. By collecting input, Java programs can customize their output, perform specific actions, or adjust their functionality to meet the needs of different users. Different Java packages contain other classes to take input from the user. This article deals with how to take input from User in Java.
Ways to Take Input from User in Java
There are three ways to take input from the user in Java programs. They are as follows-
- Using the Scanner class
- Using the BufferedReader class
- Using the Console class
The Scanner Class
The Scanner Class is used to take input from the user in Java. It is one of the most commonly used and easy ways to take input from the user. It is a part of the java.util package. We can take inputs of primitive types like int, char, float, double etc. using the Scanner class. It is much easier to read making it the most common choice.
Syntax
Scanner scn=new Scanner(System.in);
It is important to import the java.util package for using the Scanner class. In the above statement, we have created a Scanner class constructor and then passed the System.in argument which enables it to read from the standard input stream.
Scanner Class Methods
nextInt() | It is used to read and return the next integer value. |
nextFloat() | It is used to read and return the next float value. |
nextDouble() | It is used to read and return the next double value. |
next() | It is used to read and return the next one word value as String. |
nextLine() | It is used to read and return the next multiple word values as String |
nextLong() | It is used to read and return the next long value. |
nextShort() | It is used to read and return the next short value. |
nextByte() | It is used to read and return the next byte value. |
nextBoolean() | It is used to read and return the next Boolean value. |
Example
Following is an example to use the Scanner Class in Java to take the user input.
//import the java.util package import java.util.*; public class Main < public static void main(String args[]) < // to take the input, use the Scanner Class Scanner scn = new Scanner(System.in); System.out.println("Enter your roll no"); int roll_no = scn.nextInt(); //integer input System.out.println("Enter your name"); String name= scn.next(); //String input System.out.println("Enter your marks"); double marks = scn.nextDouble(); //double input System.out.println("Your roll no is- " + roll_no+" your name is- "+name+" and marks- "+marks); >>
Output
Following is the output of the above code
Enter your roll no 1 Enter your name Sita Enter your marks 99 Your roll no is- 1 your name is- Sita and marks- 99.0
Explanation
In the above code, we have created a Scanner Class in Java. We have taken three different user inputs in the form of int, String and double. After taking the inputs, we printed them.
Advantages of Scanner class
Disadvantages of Scanner class
The BufferedReader class
BufferedReader is another class in Java to take input from the user. It was introduced in JDk 1.0 and is the basic method to take input from the user. It is present in the java.io package.
Syntax
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
This means we have created a BufferedReader object. The InputStreamReader class is used to convert the input byte stream into a character stream as Bufferedreader understands it. System.in is the standard input which is the keyboard by default.
The readLine() method is used to read the line and store it as a String. If the String has to be converted to other data types, it has to be explicitly type casted.
Example
Following is an example to use the BufferedReader Class in Java to take the user input.
//import the java.io package import java.io.*; public class Main < public static void main(String args[]) throws IOException < // to take the input, use the BufferedReader Class BufferedReader br= new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter your roll no"); int roll_no = Integer.parseInt(br.readLine()); //integer input System.out.println("Enter your name"); String name= br.readLine(); //String input System.out.println("Enter your marks"); double marks = Double.parseDouble(br.readLine()); //double input System.out.println("Your roll no is- " + roll_no+" your name is- "+name+" and marks- "+marks); >>
Output
Following is the output of the above code —
Enter your roll no 1 Enter your name Sita Enter your marks 94 Your roll no is- 1 your name is- Sita and marks- 94.0
Explanation
A BufferedReader object called «br» is created. It takes input from the user using the InputStreamReader class, which converts the bytes of input into characters. We have taken input from the user, but the default value of it is a string. It is then type cast to the desired data type.
Advantages of BufferedReader class
Disadvantages of BufferedReader class
- Only string inputs can be taken. They have to be converted later to the desired data type.
- Multiple libraries might be needed to be imported to use the BufferedReader class.
The Console class
Using the Console class is another way to take input from the user through command line. This was introduced in JDK 1.5 and is present in the java.io package. The Console class is very useful to read passwords from the console. Also, its instantiation is simple as compared to other methods. The java.io.Console library is essential for the Console class.
Syntax
Example
Following is an example to use the Console Class in Java to take the user input.
//import the java.io package import java.io.*; public class Main < public static void main(String args[]) throws IOException < System.out.println("Enter your roll no"); int roll_no = Integer.parseInt(System.console().readLine()); //integer input System.out.println("Enter your name"); String name = System.console().readLine(); System.out.println("Enter your marks"); double marks = Double.parseDouble(System.console().readLine()); //double input System.out.println("Your roll no is- " + roll_no+" your name is- "+name+" and marks- "+marks); >>
Output
Following is the output of the above code —
Enter your roll no 1 Enter your name Sita Enter your marks 96 Your roll no is- 1 your name is- Sita and marks- 96.0
Explanation
In the above code, we have directly read the user input using the System.console() function.
Advantages of Console class
- We can enter the password securely as the password is not visible during typing.
- This class uses synchronized methods.
Disadvantages of BufferedReader class
Hence, we understood how to take input from user in Java. Depending on the preference, we can use either of them.
Java User Input (Scanner)
The Scanner class is used to get user input, and it is found in the java.util package.
To use the Scanner class, create an object of the class and use any of the available methods found in the Scanner class documentation. In our example, we will use the nextLine() method, which is used to read Strings:
Example
import java.util.Scanner; // Import the Scanner class class Main < public static void main(String[] args) < Scanner myObj = new Scanner(System.in); // Create a Scanner object System.out.println("Enter username"); String userName = myObj.nextLine(); // Read user input System.out.println("Username is: " + userName); // Output user input >>
If you don’t know what a package is, read our Java Packages Tutorial.
Input Types
In the example above, we used the nextLine() method, which is used to read Strings. To read other types, look at the table below:
Method | Description |
---|---|
nextBoolean() | Reads a boolean value from the user |
nextByte() | Reads a byte value from the user |
nextDouble() | Reads a double value from the user |
nextFloat() | Reads a float value from the user |
nextInt() | Reads a int value from the user |
nextLine() | Reads a String value from the user |
nextLong() | Reads a long value from the user |
nextShort() | Reads a short value from the user |
In the example below, we use different methods to read data of various types:
Example
import java.util.Scanner; class Main < public static void main(String[] args) < Scanner myObj = new Scanner(System.in); System.out.println("Enter name, age and salary:"); // String input String name = myObj.nextLine(); // Numerical input int age = myObj.nextInt(); double salary = myObj.nextDouble(); // Output input by user System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Salary: " + salary); >>
Note: If you enter wrong input (e.g. text in a numerical input), you will get an exception/error message (like «InputMismatchException»).
You can read more about exceptions and how to handle errors in the Exceptions chapter.
Java input value user
Learn Latest Tutorials
Preparation
Trending Technologies
B.Tech / MCA
Javatpoint Services
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
- Website Designing
- Website Development
- Java Development
- PHP Development
- WordPress
- Graphic Designing
- Logo
- Digital Marketing
- On Page and Off Page SEO
- PPC
- Content Development
- Corporate Training
- Classroom and Online Training
- Data Entry
Training For College Campus
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week
Like/Subscribe us for latest updates or newsletter