0% found this document useful (0 votes)
29 views

How To Read Java Console Input

There are three ways to read input from the Java console: using the BufferedReader class to read input as strings, using the Scanner class to read multiple data types, and using the Console class to directly read from standard input. The BufferedReader example demonstrates reading a string using readLine(), while the Scanner example shows reading a string, integer, and float. The Console class example simply reads a string input.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

How To Read Java Console Input

There are three ways to read input from the Java console: using the BufferedReader class to read input as strings, using the Scanner class to read multiple data types, and using the Console class to directly read from standard input. The BufferedReader example demonstrates reading a string using readLine(), while the Scanner example shows reading a string, integer, and float. The Console class example simply reads a string input.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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. }

You might also like