In Java, there are four different ways for reading input from the user in the command line environment(console).
- 1.Using Buffered Reader Class. This is the Java classical method to take input, Introduced in JDK1.
- Implementation: Java.
- Input: Geek.
- Output: Geek.
- Note:
- Using Scanner Class.
- Input:
- Output:
Similarly one may ask, how many ways can you read Java console input and write two ways program?
In Java, there are three ways to read input from a console : System. console (JDK 1.6) Scanner (JDK 1.5)
- System. console.
- Scanner. Before JDK 1.6, this is the Scanner way to read input from the console.
- BufferedReader + InputStreamReader.
Secondly, how read and write from console in Java? Java Console Example
- import java.io.Console;
- class ReadStringTest{
- public static void main(String args[]){
- Console c=System.console();
- System.out.println("Enter your name: ");
- String n=c.readLine();
- System.out.println("Welcome "+n);
- }
Also know, what is the console method used to read input from a user?
Using Scanner class
It is probably the best choice of taking console input from the user. This class reads the input from the user in the console or the command line.
How do you read input in Java?
Let's see another example, in which we have taken string input.
- import java.util.*;
- class UserInputDemo1.
- {
- public static void main(String[] args)
- {
- Scanner sc= new Scanner(System.in); //System.in is a standard input stream.
- System.out.print("Enter a string: ");
- String str= sc.nextLine(); //reads string.
