Java Programming Handbook

    Java Basic Input and Output

    In Java, Input and Output (I/O) operations allow us to interact with the outside world, whether it's reading from or writing to a file, console, or even a network. Understanding the basics of I/O in Java is essential for any beginner programmer, as it forms the foundation for user interaction and data management in Java applications.

    Types of I/O in Java#

    Java provides two main types of I/O:

    • Byte-oriented I/O (for handling binary data, such as images or video files)
    • Character-oriented I/O (for handling text data)

    In this blog, we'll focus on the basics of console-based I/O using the Scanner class for input and System.out.println() for output.

    1. Basic Output with System.out.println()#

    The simplest way to print output to the console is using the System.out.println() method. This method prints a message followed by a newline.

    Example 1: Printing to the Console#

    class Main { public static void main(String[] args) { // Printing a message to the console System.out.println("Hello, World!"); // Output: Hello, World! } }

    Explanation:

    • System.out.println() prints the string "Hello, World!" to the console.
    • println() means "print line", so it automatically moves to the next line after printing.

    Expected Output:

    Hello, World!

    2. Reading Input from the Console using Scanner#

    To read input from the user, we use the Scanner class. This class is part of the java.util package and allows us to capture different types of input, such as strings, integers, and floating-point numbers.

    Example 2: Reading User Input#

    import java.util.Scanner; class Main { public static void main(String[] args) { // Create a Scanner object to read input from the console Scanner scanner = new Scanner(System.in); // Ask the user for their name System.out.print("Enter your name: "); String name = scanner.nextLine(); // Read the entire line of input // Output the name entered by the user System.out.println("Hello, " + name + "!"); // Close the scanner scanner.close(); } }

    Explanation:

    • We import the Scanner class from java.util.
    • We create a Scanner object, scanner, to read input from System.in (the standard input stream, i.e., the keyboard).
    • The nextLine() method reads an entire line of text input by the user.
    • After reading the input, we print a greeting using System.out.println().

    Expected Output:

    Enter your name: John Hello, John!

    3. Reading Different Types of Data#

    The Scanner class provides methods for reading different types of data, such as integers, floating-point numbers, and booleans.

    Example 3: Reading Integer Input#

    import java.util.Scanner; class Main { public static void main(String[] args) { // Create a Scanner object Scanner scanner = new Scanner(System.in); // Ask the user to enter an integer System.out.print("Enter an integer: "); int num = scanner.nextInt(); // Reads the next integer // Output the number entered by the user System.out.println("You entered: " + num); // Close the scanner scanner.close(); } }

    Explanation:

    • We use nextInt() to read an integer input from the user.
    • System.out.println() is used to display the entered value.

    Expected Output:

    Enter an integer: 42 You entered: 42

    Example 4: Reading a Floating-Point Number#

    import java.util.Scanner; class Main { public static void main(String[] args) { // Create a Scanner object Scanner scanner = new Scanner(System.in); // Ask the user to enter a floating-point number System.out.print("Enter a floating-point number: "); double num = scanner.nextDouble(); // Reads the next double // Output the number entered by the user System.out.println("You entered: " + num); // Close the scanner scanner.close(); } }

    Explanation:

    • We use nextDouble() to read a floating-point number from the user.
    • System.out.println() displays the entered number.

    Expected Output:

    Enter a floating-point number: 3.14 You entered: 3.14

    4. Reading Multiple Inputs#

    You can read multiple values in a single line using the Scanner class. Below is an example that reads an integer and a string in one line.

    Example 5: Reading Multiple Inputs#

    import java.util.Scanner; class Main { public static void main(String[] args) { // Create a Scanner object Scanner scanner = new Scanner(System.in); // Ask the user to enter an integer and a string System.out.print("Enter an integer and a name: "); // Read the integer and the string int age = scanner.nextInt(); // Read the integer scanner.nextLine(); // Consume the leftover newline character String name = scanner.nextLine(); // Read the string // Output the inputs System.out.println("Name: " + name + ", Age: " + age); // Close the scanner scanner.close(); } }

    Explanation:

    • After reading the integer using nextInt(), we need to call scanner.nextLine() to consume the newline character left behind. This allows us to read the string correctly.
    • The input values are then printed using System.out.println().

    Expected Output:

    Enter an integer and a name: 25 John Name: John, Age: 25

    5. Formatting Output#

    Java also provides the printf() method for formatting output. It allows you to control the output format, such as the number of decimal places or the width of the output.

    Example 6: Formatting Output with printf()#

    java CopyEdit class Main { public static void main(String[] args) { // Declare variables double price = 23.456; // Format output using printf System.out.printf("The price is: %.2f\\n", price); // Output: The price is: 23.46 } }

    Explanation:

    • %.2f formats the floating-point number to two decimal places.
    • The printf() method provides more control over how data is presented.

    Expected Output:

    The price is: 23.46

    Conclusion#

    In this blog, we have covered the basics of Input and Output in Java. We learned how to use:

    • System.out.println() to display output.
    • Scanner class to take input from the user.
    • Different methods like nextLine(), nextInt(), and nextDouble() to read various data types.
    • How to format output using printf().

    Last updated on Apr 09, 2025