Java Input and Output: Scanner, println and printf

Updated on October 22, 2026

Input and output in Java is the first thing you need: a simple program reads something, calculates with it and prints a result. That's all you need for the first tasks.

The frame is always the same. Your code goes between the curly braces of main, and Java runs it line by line from top to bottom. What public class and static mean exactly is explained in the article on classes and objects in Java. Until then, just leave the frame as it is.

Output and variables

System.out.println(...) prints a line. A variable is a named place that stores a value. When you create one, you put its type in front:

  • int for whole numbers, e.g. int age = 19;
  • double for decimal numbers, e.g. double price = 4.99; (with a dot, not a comma)
  • String for text in double quotes, e.g. String name = "Ada"; (more on this in strings in Java)

+ joins texts and values together. Every statement ends with a semicolon.

public class Main {    public static void main(String[] args) {        String name = "Ada";        int age = 19;        System.out.println("Hello, " + name + "!");        System.out.println(name + " is " + age + " years old.");        System.out.println("Next year: " + (age + 1));    }}

Reading input and calculating

A Scanner reads what someone types into the console. nextInt() gets the next whole number, nextDouble() the next decimal number and nextLine() a whole line of text. Spaces and line breaks between numbers don't matter.

Java calculates with +, -, *, / and %. Two things often surprise beginners:

  • If you divide two int values, the decimal places are dropped: 17 / 5 gives 3.
  • % (modulo) gives the remainder of the division: 17 % 5 gives 2.

As soon as a decimal number is involved, Java keeps the decimal places, for example / 2.0 instead of / 2.

import java.util.Scanner;
public class Main {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        int a = scanner.nextInt();        int b = scanner.nextInt();        System.out.println(a + " / " + b + " = " + a / b);        System.out.println(a + " % " + b + " = " + a % b);        System.out.println("Average: " + (a + b) / 2.0);    }}
Input
17 5

Under “Input” you see what the program reads.

Nice formatting with printf

Decimal numbers often have lots of decimal places. System.out.printf lets you decide how a value looks. The text contains placeholders that are replaced, in order, by the values after it:

  • %.2f: decimal number with exactly two decimal places (rounded)
  • %d: whole number
  • %s: text
  • %n: line break (printf doesn't add one by itself)
import java.util.Scanner;
public class Main {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        double price = scanner.nextDouble();        double withTax = price * 1.19;        System.out.println("Without printf: " + withTax);        System.out.printf("With printf: %.2f €%n", withTax);    }}
Input
12.5

Common mistakes

  • Integer division

    double average = (2 + 3) / 2; gives 2.0, not 2.5. The division happens with two int values, and only then is the result stored in a double. Fix: divide by 2.0.

  • Plus joins text

    Once there's a text on the left, + just appends everything:

    System.out.println("Sum: " + 2 + 3);   // Sum: 23System.out.println("Sum: " + (2 + 3)); // Sum: 5

    So put calculations in the output in parentheses.

  • Typos the compiler reports

    Java is case-sensitive: it doesn't know system.out.println or string. If a semicolon is missing, the compiler reports ';' expected. The line number in the message usually points to that spot or the line before it.

  • The output has to match exactly

    The tests compare your output character by character. A missing space, an extra comma or print instead of println is enough to fail a test. Compare your output carefully with the task's example. As soon as the program should print something different depending on the input, you need conditions in Java.

Tasks on this topic

Practise what you learned right in your browser. Tests check your solution instantly.