Java Console I/O Tutorial

Focusing specifically on console input and output in Java, let’s explore how to read various types of data from the console and utilize different printing options to display output effectively. These operations are crucial for interactive Java applications.

Reading Input from the Console

Java uses the Scanner class from the java.util package for reading console input. The Scanner class provides methods to read different types of data, including int, double, String, and more.

Reading a String

Reading an Integer

Reading a Double

Printing Options in Java

Java provides several methods for printing to the console, allowing for simple text output, formatted strings, and more.

Using System.out.println()

Prints the message to the console followed by a newline.

Using System.out.print()

Prints the message to the console without adding a newline at the end.

Using System.out.printf()

Allows for formatted output, similar to the printf function in C/C++.

Reading and Printing Multiple Data Types Together

These examples showcase the flexibility of the Scanner class for reading various data types from the console and the versatility of Java’s print methods for outputting text. Understanding these basic I/O operations is essential for building interactive Java applications.

Leave a Reply

Your email address will not be published. Required fields are marked *