In Java, there are four different ways to read user input in the command line environment (console).
1. Use buffered reader class
This is the classic Java input method, introduced in JDK1.0. This method is used by wrapping System.in (standard input stream) in InputStreamReader, and InputStreamReader is wrapped in BufferedReader, and we can read user input on the command line.
Input is buffered for efficient reading.
Wrapper codes are hard to remember.
implement:
// Java program to demonstrate BufferedReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args)
throws IOException
{
// Enter data using BufferReader
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
// Reading data using readLine
String name = reader.readLine();
// Printing the read line
System.out.println(name);
}
}
enter:
尚学堂
output:
Auxiliary space: O(1)
尚学堂
2. Use the Scanner class
This is probably the most preferred method of accepting input. The main purpose of the Scanner class is to parse primitive types and strings using regular expressions, however, it can also be used to read user input on the command line.
Convenience method for parsing primitives (nextInt(), nextFloat(), ...) from tokenized input.
Regular expressions can be used to find tokens.
reading out of sync
// Java program to demonstrate working of Scanner in Java
import java.util.Scanner;
class GetInputFromUser {
public static void main(String args[])
{
// Using Scanner for Getting Input from User
Scanner in = new Scanner(System.in);
String s = in.nextLine();
System.out.println("You entered string " + s);
int a = in.nextInt();
System.out.println("You entered integer " + a);
float b = in.nextFloat();
System.out.println("You entered float " + b);
}
}
enter:
hello
12
3.4
output:
您输入了字符串 hello
您输入了整数 12
你输入了 float 3.4
3. Use the console class
It has become the preferred way of reading user input from the command line. Additionally, it can be used to read password-like input without echoing the characters entered by the user; format string syntax (such as System.out.printf() ) can also be used.
advantage:
- Read the password without echoing the characters entered.
- The read method is synchronous.
- Format string syntax can be used.
- Not for use in non-interactive environments (such as IDEs).
// Java program to demonstrate working of System.console()
// Note that this program does not work on IDEs as
// System.console() may require console
public class Sample {
public static void main(String[] args)
{
// Using Console to input data from user
String name = System.console().readLine();
System.out.println("You entered string " + name);
}
}
enter:
hello
output:
您输入了字符串 hello
4. Using command line parameters
Most commonly used for user input for competitive encoding. Command-line arguments are stored in string format. The parseInt method of the Integer class converts the string argument to an Integer. Likewise for floats and others during execution. The usage of args[] appears in this input form. The transfer of information occurs during program execution. The command line is given to args[]. These programs must be run on cmd.
code:
// Program to check for command line arguments
class Hello {
public static void main(String[] args)
{
// check if length of args array is
// greater than 0
if (args.length > 0) {
System.out.println(
"The command line arguments are:");
// iterating the args array and printing
// the command line arguments
for (String val : args)
System.out.println(val);
}
else
System.out.println("No command line "
+ "arguments found.");
}
}
Command line parameters:
javac GFG1.java
java Main Hello World
output:
命令行参数是:
你好
世界