How to Read Java Console Input
There are three different ways to read the input from Java Console, they are –
Using Java Bufferedreader Class
Scanner Class in Java
Console Class in Java
Example of Java Bufferedreader Class–
1. import java.io.BufferedReader;
2. import java.io.IOException;
3. import java.io.InputStreamReader;
4. public class Test
5. {
6. public static void main(String[] args) throws IOException
7. {
8. BufferedReader reader =
9. new BufferedReader(new InputStreamReader(System.in));
10. String name = reader.readLine();
11. System.out.println(name);
12. }
13. }
Scanner Class
1. import java.util.Scanner;
2. class GetInputFromUser
3. {
4. public static void main(String args[])
5. {
6. Scanner in = new Scanner(System.in);
7. String s = in.nextLine();
8. System.out.println("You entered string "+s);
9. int a = in.nextInt();
10. System.out.println("You entered integer "+a);
11. float b = in.nextFloat();
12. System.out.println("You entered float "+b);
13. }
14. }
Console
1. public class Sample
2. {
3. public static void main(String[] args)
4. {
5. // Using Console to input data from user
6. String name = System.console().readLine();
7. System.out.println(name);
8. }
9. }