In the Java program, there are 3 ways we can read input from the user in the command line environment to get user input, Java BufferedReader Class, Java Scanner Class, and Console class. Let us discuss the classes in detail. We use the Scanner class to obtain user input. This program asks the user to enter an integer, a string, and float, and it will be printed on display. The scanner class in java.util is present so that we can add this package to our software. First, we create a Scanner Class object and use the Scanner Class method.
There are three ways to read the User Input:
ADVERTISEMENT Popular Course in this category JAVA MASTERY - Specialization | 78 Course Series | 15 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
These three class are mentioned below; let us discuss them in detail:
It extends reader class. BufferedReader reads input from the character-input stream and buffers characters so as to provide an efficient reading of all the inputs. The default size is large for buffering. When the user makes any request to read, the corresponding request goes to the reader, and it makes a read request of the character or byte streams; thus, BufferedReader class is wrapped around another input streams such as FileReader or InputStreamReaders.
For example:
BufferedReader reader = new BufferedReader(new FileReader("foo.in"));
BufferedReader can read data line by line using the method readLine() method.
BufferedReader can make the performance of code faster.
BufferedReader has two constructors as follows:
1. BufferedReader(Reader reader): Used to create a buffered input character stream that uses the default size of an input buffer.
2. BufferedReader(Reader reader, input size): Used to create a buffered input character stream that uses the size provided for an input buffer.
When the user enters the character from the keyboard, it gets read by the device buffer and then from System.in it passes on to the buffered reader or input stream reader and gets stored in the input buffer.
Code:
import java.util.*; import java.lang.*; import java.io.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /*package whatever //do not write package name here */ class BufferedReaderDemo { public static void main (String[] args) throws NumberFormatException, IOException { System.out.println("Enter your number"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int t = Integer.parseInt(br.readLine()); System.out.println("Number you entered is: " + t); System.out.println("Enter your string"); String s = br.readLine(); System.out.println("String you entered is: " + s); } }
Output:
Program with reading from InputStreamReader and BufferedReader:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class BufferedReaderDemo { public static void main(String args[]) throws IOException{ InputStreamReader reader = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(reader); System.out.println("What is your name?"); String name=br.readLine(); System.out.println("Welcome "+name); } }
Output:
java.util. scanner class is one of the classes used to read user input from the keyboard. It is available at the util package. Scanner classes break the user input using a delimiter that is mostly whitespaces by default. The scanner has many methods to read console input of many primitive types such as double, int, float, long, Boolean, short, byte, etc. It is the simplest way to get input in java. Scanner class implements Iterator and Closeable interfaces. The scanner provides nextInt() and many primitive type methods to read inputs of primitive types. The next() method is used for string inputs.
Below are mentioned the method to scan the primitive types from console input through Scanner class.
Program to read from Scanner Class:
Using scanner class. import java.util.Scanner; /*package whatever //do not write package name here */ class ScannerDemo { public static void main (String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter your number"); int t = sc.nextInt(); System.out.println("Number you entered is: " + t); System.out.println("Enter your string"); String s = sc.next(); System.out.println("String you entered is: " + s); } }
Output:
Using the console class to read the input from the command-line interface. It does not work on IDE.
Code:
public class Main { public static void main(String[] args) { // Using Console to input data from user System.out.println("Enter your data"); String name = System.console().readLine(); System.out.println("You entered: "+name); } }
Output:
The above is the detailed content of Java User Input. For more information, please follow other related articles on the PHP Chinese website!