Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
64 views

How To Read Java Console Input: Example of Java Bufferedreader Class

There are three ways to read input from the Java console: using the BufferedReader class, Scanner class, or Console class. The BufferedReader example demonstrates reading a line of input and storing it in a String variable. The Scanner class example shows reading different data types, including an integer, float, and string. The Console class reads a line of input and stores it in a String similar to the BufferedReader approach.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

How To Read Java Console Input: Example of Java Bufferedreader Class

There are three ways to read input from the Java console: using the BufferedReader class, Scanner class, or Console class. The BufferedReader example demonstrates reading a line of input and storing it in a String variable. The Scanner class example shows reading different data types, including an integer, float, and string. The Console class reads a line of input and stores it in a String similar to the BufferedReader approach.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
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