0% found this document useful (0 votes)
2 views3 pages

Using Java BufferedReader Class

The document explains various methods for reading user input in Java, including the BufferedReader, command-line arguments, DataInputStream, and Console class. Each method is illustrated with example code and output demonstrating how to capture and display user input. The document highlights the efficiency and use cases of each approach for handling input in Java applications.

Uploaded by

pmahakalkar
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)
2 views3 pages

Using Java BufferedReader Class

The document explains various methods for reading user input in Java, including the BufferedReader, command-line arguments, DataInputStream, and Console class. Each method is illustrated with example code and output demonstrating how to capture and display user input. The document highlights the efficiency and use cases of each approach for handling input in Java applications.

Uploaded by

pmahakalkar
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/ 3

o Using Java BufferedReader class:

The BufferedReader class, available in the java.io package, provides more efficient input reading
compared to Scanner, especially for reading strings. It can be used in combination with
InputStreamReader and System.in to read user input. Here's an example of using BufferedReader to
read a line of text:

InputExample.java

1. import java.io.BufferedReader;

2. import java.io.IOException;

3. import java.io.InputStreamReader;

4. public class InputExample {

5. public static void main(String[] args) throws IOException {

6. BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

7. System.out.print("Enter a line of text: ");

8. String input = reader.readLine();

9. System.out.println("You entered: " + input);

10. }

11. }

Output:

Enter a line of text: Hello, World!

You entered: Hello, World!

o Using Java Command-line arguments:

Java allows you to pass command-line arguments to your program during its execution. These
arguments can be accessed using the args parameter in the main method. Command-line arguments
are useful when you want to provide inputs before running the program. Here's an example:

InputExample.java

1. public class InputExample {

2. public static void main(String[] args) {

3. if (args.length > 0) {

4. String input = args[0];

5. System.out.println("You entered: " + input);

6. } else {

7. System.out.println("No input provided.");

8. }
9. }

10. }

Output:

You entered: Hello

If no command line argument is provided, the program displays the message "No input provided".
such as manufacturing. For example, if the assignment is executed without arguments, the result
would be:

No input was provided.

o Using the Java DataInputStream class:

The DataInputStream class, part of the java.io package, allows you to read different data types from
the input stream. It provides methods like readInt(), readDouble(), readLine(), etc., to read specific
data types. Here's an example of using DataInputStream to read an integer:

InputExample.java

1. import java.io.DataInputStream;

2. import java.io.IOException;

3. import java.io.InputStream;

4. public class InputExample {

5. public static void main(String[] args) throws IOException {

6. InputStream inputStream = System.in;

7. DataInputStream dataInputStream = new DataInputStream(inputStream);

8. System.out.print("Enter an integer: ");

9. int num = dataInputStream.readInt();

10. System.out.println("You entered: " + num);

11. }

12. }

Output:

Enter an integer:

You entered:

o Using Java Console class:

The Console class, available in Java 6 and later, provides methods for reading input from the console.
It is especially useful for simple text-based input/output interactions. Here's an example of using the
Console class to read a line of text:

InputExample.java
1. public class InputExample {

2. public static void main(String[] args) {

3. Console console = System.console();

4. if (console != null) {

5. String input = console.readLine("Enter a line of text: ");

6. System.out.println("You entered: " + input);

7. } else {

8. System.out.println("Console input is not available.");

9. }

10. }

11. }

Output:

Enter a line of text: Hello, World!

You entered: Hello, World!

The program you provide attempts to read input from the console using System.console(). The
output of the program will depend on whether the program is running in an environment that
supports console input. When the program is running in console mode (executing the program
directly from the command line), it will display a claim line of text. The user will enter text and press
Enter to print "You entered: " followed by the text entered.

You might also like