Ways to read input from Console in Java
Java provides many flexibilities to the programmers to make them provide input in various means. To read input from console, there are four different methods available in java. This article will introduce you to all the four methods to read input from the command line environment.
Ways to read input from Console in Java
1. Using Buffered Reader class in java:
This is the general method that is mostly used to read input in Java. It was introduced in JDK1.0. In this method, the standard input-output stream, System.in is wrapped in an InputStreamReader, which is further wrapped in a BufferedReader. By this, we can read the input from the user via the command line.
The main advantage of using the Buffered Reader class is that it is efficient for reading.
But on the other hand, the wrapping code is a bit hard to remember.
Sample program to read input from the console using Buffered Reader class:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class FirstCode{ public static void main(String args[]) throws IOException{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); String word = reader.readLine(); System.out.println(word); } }
Input:
Java
Output:
This format obtains string value as an input. When the data type differs, we can change accordingly as Integer.parseInt() and Double.parseDouble().
2. Using the Scanner class:
This is the most preferred method to read input. It helps us to pass the primitive data types and strings with no change in the expressions. This method also reads input from the user via the command line.
The appropriate methods for passing the primitives are nextInt(), nextFloat(), etc. The regular expressions are helpful to find tokens.
The drawback of using the Scanner class is that no synchronization in reading methods is present.
Sample program to read an input using the Scanner class:
import java.util.Scanner; class FirstCode{ public static void main(String args[]){ Scanner in = new Scanner(System.in); System.out.println(“Enter the sl.no: ”); int num = in.nextInt(); System.out.println(“Sl.no:”, +num); System.out.println(“Enter a subject: ”); String subject = in.nextline(); System.out.println(“The subject is: ”+subject); System.out.println(“Enter your marks in float:”); float marks = in.nextFloat(); System.out.println(“The marks is: ”+marks); in.close(); } }
Input:
Enter the sl.no:
1
Enter the subject:
History
Enter your marks in float:
87.3
Output:
History
87.3
3. Using Console class:
This method is easier to give command-line arguments. In situations where a password is required, this method works effectively without echoing the characters given by the user. The string syntax that can be used to achieve this is System.out.printf().
The main reason why programmers prefer this method is that passwords can be entered without echoing the characters. It allows the usage of string syntax. It synchronizes the reading methods.
But the only disadvantage is that it does not work well in a non-interactive environment like an IDE.
Sample program to read input using Console class:
public class FirstCode{ public static void main(String args[]){ String word = System.console().readLine(); System.out.println(“The word you entered is: ”+word); } }
Input:
FirstCode
Output:
4. Using Command-line argument:
The command-line argument stores the input in the String format. When the input is an integer, the parseInt() method helps in converting it. Similarly, parseFloat(), parseDouble() and other methods are available. This takes place during the run time in the command prompt.
Sample program to read input as command-line arguments:
class FirstCode{ public static void main(String args[]){ System.out.println(“The given argument is: ”+args[0]); } } Compiling: > javac FirstCode.java Run: > java FirstCode Learn
Output:
Methods of Java Console Class:
The various methods of Java Console class are listed in the table below:
Method | Description |
Reader reader() | It attains the object of the Reader class that is related to the console. |
String readLine() | We can use this method to read a single line of text from the console. |
String readLine(String fmt, Object…args) | It provides output in the formatted form before getting the input from the console. |
char[] readPassword() | It read the password displayed on the console screen. |
char[] readPassword(String fmt, Object…args) | It provides formatted output before getting the password that is not visible on the console screen. |
Console format(String fmt, Object…args) | It displays the string in a formatted manner as the output in the console stream. |
Console printf(String format, Object…args) | It prints a string to the console output stream. |
PrintWriter writer() | It gets the object of the PrintWriter class. |
void flush() | It flushes the console. |
To know further about command-line arguments, refer to the article on this topic. You can find an in-depth explanation of how to work with command-line arguments.
Conclusion:
These are the four different methods to read input from the console in Java. I hope this article explained it to you in a clear manner. You can try them in various situations to get the input for a program in Java.