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:
intfor whole numbers, e.g.int age = 19;doublefor decimal numbers, e.g.double price = 4.99;(with a dot, not a comma)Stringfor 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
intvalues, the decimal places are dropped:17 / 5gives3. %(modulo) gives the remainder of the division:17 % 5gives2.
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); }}
Input17 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 (printfdoesn'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); }}
Input12.5
Common mistakes
Integer division
double average = (2 + 3) / 2;gives2.0, not2.5. The division happens with twointvalues, and only then is the result stored in adouble. Fix: divide by2.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: 5So put calculations in the output in parentheses.
Typos the compiler reports
Java is case-sensitive: it doesn't know
system.out.printlnorstring. 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
printinstead ofprintlnis 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.
Related articles
- ConditionsJava conditions with if, else if, switch, && and || explained clearly. With examples and the common mistakes that cost points in exams.Read article
- StringsJava strings explained simply: substring, charAt, split, equals and StringBuilder with examples. Plus common mistakes like comparing with ==.Read article
- LoopsJava loops explained simply: for, while, do-while, break and continue with examples. Plus the common mistakes that cost points in exams.Read article