How To Read Java Console Input
How To Read Java Console Input
There are three different ways to read the input from Java Console, they are –
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