0% found this document useful (0 votes)
18 views2 pages

Java Console I/O Operations Guide

Uploaded by

kkasana9222
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views2 pages

Java Console I/O Operations Guide

Uploaded by

kkasana9222
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Console Input and Output in Java

In Java, console input and output operations are essential for interacting with the user. They allow
reading user inputs and displaying outputs on the screen. Java provides multiple ways to perform
these operations.

1. Output in Java:
Java provides several ways to display output on the console.

a) Using [Link]()
Prints text without a newline.
b) Using [Link]()
Prints text followed by a newline.
c) Using [Link]()
Formats the output using format specifiers similar to C language.
// Example: Output in Java
public class OutputExample {
public static void main(String[] args) {
[Link]("Hello "); // prints without newline
[Link]("World!"); // prints with newline
[Link]("Number: %d and Text: %s", 10, "Java");
}
}

2. Input in Java:
There are multiple ways to take input from the user.

a) Using Scanner Class (Most Common)


Part of [Link] package. Used for reading primitive types and strings.

b) Using BufferedReader
Part of [Link] package. Efficient for reading text data.

c) Using Console Class


Part of [Link]. Used mainly for secure inputs like passwords.
// Example: Using Scanner for input
import [Link];

public class ScannerInputExample {


public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter your name: ");
String name = [Link]();
[Link]("Enter your age: ");
int age = [Link]();
[Link]("Hello " + name + ", age " + age);
}
}

// Example: Using BufferedReader for input


import [Link];
import [Link];
import [Link];

public class BufferedReaderExample {


public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("Enter your city: ");
String city = [Link]();
[Link]("City entered: " + city);
}
}
// Example: Using Console for input
public class ConsoleExample {
public static void main(String[] args) {
[Link] console = [Link]();
if (console != null) {
String username = [Link]("Enter username: ");
char[] password = [Link]("Enter password: ");
[Link]("Username: " + username);
} else {
[Link]("No console available.");
}
}
}

Summary:
- Use [Link] for output.
- Use Scanner for general input.
- Use BufferedReader for efficient text input.
- Use Console for secure input operations.

These input/output methods form the foundation of user interaction in Java applications.

You might also like