User Input In Java | Java Tutorial
Sometimes, we may face a situation where we need to get the input from the user in run-time. We use “Scanner” class to accept input from the user. How to take input from a user in Java is a popular interview question. We use Scanner class in Java to get input from the user. Java Scanner class comes under java.util.package. To use Scanner class, we need to import the java.util.package in our program.
Syntax:
import java.util.Scanner; // Creating an object of Scanner class Scanner userInput = new Scanner(System.in); variable = userInput.next(); userInput.close();
Sample Program:
package classOneGeneral; //We need to import Scanner class to accept input from the user import java.util.Scanner; public class UserInput { public static void main(String [] args){ // Creating an instance "userInput" of a Scanner class Scanner userInput = new Scanner(System.in); System.out.println("Learning Java from?"); // Using nextLine method to get the input and move the cursor to the new line /*nextLine() method Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line. Since this method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.*/ String website = userInput.nextLine(); // Close the scanner object using close() method to prevent memory leak userInput.close(); System.out.println("I am learning Java from "+ website); } }
Other ways to read input from the user in Java are as follows. In Java, there are three different ways to read input from the user.
- Scanner Class
- BufferedReader Class
- Console Class
We have already seen Scanner Class above. Even though Scanner Class is most preferred way to get user input in java there are other two ways to read input from the user in Java. Let’s see those now.
BufferedReader Class:
Java BufferedReader class is used to read text from input stream. It is used to buffer characters for efficient handling of characters, arrays and strings. It reads data line by line by using readLine() method. It gives fast performance.
Sample Program:
package myPackage; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BufferedReaderClass { public static void main(String[] args) throws IOException { // Instantiate BufferedReader object BufferedReader myReader = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Input your site name :"); // To read data using readLine method. Here I will pass SoftwareTestingMaterial.com String mySite = myReader.readLine(); // To print the my site name System.out.println("Site name is : "+ mySite); } }
Input:
SoftwareTestingMaterial.com
Output:
Site name is : SoftwareTestingMaterial.com
Console Class:
The Console class was introduced in Java 1.6. It is one of the preferred way by developers for reading user input from the command line. This Console class provide methods like readLine() and readPassword(). Using this readPassword method the user input will not be shown in the console. The password will be returned as an array of char.
Note: It doesn’t work on IDE’s because System.console() requires console.
package myPackage; import java.io.IOException; public class BufferedReaderClass { public static void main(String[] args) throws IOException { // Using Console to input data from user String mySite = System.console().readLine(); System.out.println(mySite); } }
You may also like:
I want to be a software tester.Need some suggestions
Hi Idrees Javid, use our contact form to contact us.