Input and Output in Java

Subject: Data Structures and Algorithms · Language: Java · Level: beginner · 6 min read

Learn Scanner, print, println, printf, reading numbers, words, and full lines in Java.

Input means taking data from the user or from a test case. Output means showing the result produced by the program. In coding practice, the compiler usually receives input values, runs the program, and then shows the output so the student can check the answer.

In Java beginner programs, input is commonly handled using the `Scanner` class, and output is printed using `System.out`. Once you understand input and output properly, you can solve problems where values are not fixed inside the program but are entered by the user.

Output in Java

Java provides different output methods because sometimes we want to print on the same line, sometimes on a new line, and sometimes in a fixed format. `System.out.print()` prints the given value and keeps the cursor on the same line. It is useful when you want the next output to continue beside the current output.

`System.out.println()` prints the given value and then moves the cursor to the next line. This is the most commonly used output method in beginner programs. `System.out.printf()` prints formatted output. It is useful when you want controlled output, such as printing an integer, decimal value, or text in a specific format.

```java

System.out.print("Hello ");

System.out.println("Java");

System.out.printf("Marks = %d", 90);

```

In this example, `print` prints `Hello` without changing the line, `println` prints `Java` and moves to the next line, and `printf` prints the marks using `%d` as a placeholder for an integer.

Input Using Scanner

`Scanner` is a built-in Java class used to read input. Since it belongs to the `java.util` package, we import it before using it.

```java

import java.util.*;

Scanner sc = new Scanner(System.in);

``` Here, `sc` is the Scanner object, and `System.in` means the input source is the keyboard or the input area provided by the compiler. After creating this object, we can use Scanner methods to read different types of values.

Common Scanner Methods

Use `next()` when the input is a single word, such as a first name. Use `nextLine()` when the input may contain spaces, such as a full name or sentence.

Reading Numbers and Text Together

One common beginner problem happens when `nextLine()` is used after methods like `nextInt()` or `nextDouble()`. Numeric methods read only the number, but the newline after pressing Enter may remain in the input buffer. Because of this, the next `nextLine()` may read an empty line. To handle this, read the leftover newline once before taking the actual full-line input.

```java

int age = sc.nextInt();

sc.nextLine();

String fullName = sc.nextLine();

```

Here, the extra `sc.nextLine()` clears the leftover newline. Then the second `nextLine()` correctly reads the full name.

Formatted Output

`printf` is useful when output must follow a specific format. In DSA, most problems do not require heavy formatting, but it is still useful for decimal values and clean reports.

```java

double percentage = 87.456;

System.out.printf("Percentage = %.2f", percentage);

```

The format `%.2f` means print the decimal value with two digits after the decimal point. So the output becomes `Percentage = 87.46`.

Example Program

```java

import java.util.*;

public class Main {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in); System.out.print("Enter name: ");

String name = sc.next(); System.out.print("Enter marks: ");

int marks = sc.nextInt(); System.out.println("Name = " + name);

System.out.println("Marks = " + marks);

}

}

``` This program first creates a Scanner object. Then it reads one word as the student's name and one integer as marks. Finally, it prints both values in a clear format.

Input and Output in DSA

In DSA problems, input is often a number, an array size, array elements, a string, or multiple test cases. Output is usually the final answer, such as maximum value, count, index, sorted array, or yes/no result. For small beginner programs, `Scanner` is easy to understand. For very large input, Java programmers often use faster input methods, but `Scanner` is the right starting point because it teaches input clearly.

Practice Question

Take a student's name, roll number, and marks as input. Print the details in separate lines, and also print whether the student passed if marks are greater than or equal to 40.

More Java Lessons

Browse all PrepCampus study materials