Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java ReferenceJava Reference1

Java Basic Input and Output
Scanner, System.in, System.out



Basic Input and Output in Java

When you first write a Java program, it’s natural to want interaction — a two-way conversation between the user and the machine. Java enables this through standard input and output streams. This tutorial will walk you through how to accept user input and print output using beginner-friendly and practical approaches.

Java Output – Writing to Console

Let’s begin with the most common way to display information in Java: System.out.print() and System.out.println().

Using System.out.print() and println()

public class HelloWorld {
    public static void main(String[] args) {
        System.out.print("Hello ");
        System.out.print("World");
        System.out.println("!");
    }
}
Hello World!

Explanation:

System.out.print() prints without moving to the next line, whereas System.out.println() adds a new line after printing. It's essential to use the right one depending on whether you want continued or separate output lines.

Java Input – Reading from User

To accept input from the keyboard, Java commonly uses the Scanner class from the java.util package.

Step 1: Import the Scanner Class

import java.util.Scanner;

Step 2: Create Scanner Object

You create a Scanner object using:

Scanner input = new Scanner(System.in);

Example: Reading User Input

import java.util.Scanner;

public class UserInputExample {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = input.nextLine();
        System.out.println("Hello, " + name + "!");
    }
}
Enter your name: Alice
Hello, Alice!

Explanation:

input.nextLine() reads a full line of text. Scanner provides different methods for different types of input:

Reading Different Types of Input

Example: Reading Numbers

import java.util.Scanner;

public class SumCalculator {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter first number: ");
        int num1 = input.nextInt();
        System.out.print("Enter second number: ");
        int num2 = input.nextInt();
        int sum = num1 + num2;
        System.out.println("Sum: " + sum);
    }
}
Enter first number: 10
Enter second number: 20
Sum: 30

Checks and Validations

While using nextInt(), if the user inputs something non-numeric, your program will throw an InputMismatchException. To handle such cases, use input validation:

if (input.hasNextInt()) {
    int value = input.nextInt();
} else {
    System.out.println("Invalid input. Please enter a number.");
}

Common Input Mistakes and How to Avoid Them

Problem: nextLine() after nextInt()

If you use nextLine() after nextInt(), you might skip input. This happens because nextInt() doesn’t consume the newline character. Use an extra nextLine() to clear the buffer:

input.nextLine(); // consume leftover newline

Wrapping Up

Input and output are the lifeline of any interactive program. Mastering these early will help you write programs that engage users. From reading simple text to complex validations — practice makes perfect.

Key Takeaways

QUIZ

Question 4:What will be the output of the following code?

System.out.print("Java ");
System.out.print("is ");
System.out.println("fun!");

Question 5:Using nextInt() followed by nextLine() can sometimes result in skipping user input.

Question 6:Which of the following statements about Java's Scanner class are accurate?



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M