Muhammad Fari Madyan
Hire me
Java basics · part 6 of 21

Learn Java from Scratch Part 6: Reading User Input — IO.readln, Integer.parseInt & Scanner

The sixth part of a Java series for complete beginners: read what the user types with IO.readln, turn text into a number with Integer.parseInt, meet Scanner, build a birth year program, and avoid three traps such as the leftover Enter after nextInt.

Muhammad Fari Madyan

· 7 min read

Baca dalam Bahasa Indonesia →

Introduction: A Program That Listens

All our programs so far are like a speech: they talk and never listen.

In this part we build a program that asks a question, waits, and then answers based on what you type.

After this part, you will be able to:

  1. Read what the user types with IO.readln.
  2. Turn that text into a number with Integer.parseInt.
  3. Recognise Scanner, the older way that you still see in many books and courses.

Answer to the Part 5 Challenge

The question: println(10 / 4 * 2). What is the output?

The answer is B: 4.

Division and multiplication are equally strong, so they are done from the left. 10 / 4 is an int division: 2.5 loses its decimals and becomes 2. Then 2 * 2 is 4.

If you answered B, well done!


1. The Problem: A Program That Never Changes

A program that cannot take anything in can only do the same thing every time it runs.

  • A calculator needs your numbers.
  • A game needs your guess.
  • A form needs your name.

What the user gives to a program is called input.


2. Input Is a Funnel on Top of a Machine

Picture a program as a machine with a funnel on top.

The user pours typed text into the funnel. The program catches it in a box, which is a variable.

In Java 25, the simplest funnel is IO.readln. When the execution arrow reaches it, three things happen:

  1. The program stops and waits.
  2. The user types and then presses Enter.
  3. The typed text is given back as a string.

Input is like a funnel: typed text comes in as a string, then parseInt turns it into an int

Click the diagram to open it in full size.


3. Asking and Answering

Create a file named Sapa.java. Sapa means "greet".

JAVA
void main() {
    String nama = IO.readln("Siapa namamu? ");
    IO.println("Halo, " + nama + "!");
}

The text inside the brackets of readln is the question. Here it means "What is your name?". Run the program with java Sapa.java. It shows the question and then waits quietly.

Type Rani and press Enter:

TEXT
Siapa namamu? Rani
Halo, Rani!

The word Rani goes into the nama box. Then it is joined with the + sign, as in Part 4.

Run it again and type a different name. It is one program, but the result is different for every person who runs it.


4. When You Ask for a Number

What if you ask for a number, for example an age?

Be careful. readln always gives a string. The typed 17 comes in as text beads, not as a number.

To turn it into an int, use Integer.parseInt:

JAVA
void main() {
    int umur = Integer.parseInt(IO.readln("Umur: "));
    IO.println("Tahun depan umurmu " + (umur + 1));
}
TEXT
Umur: 17
Tahun depan umurmu 18

The program asks for the age (umur) and answers "Next year your age is 18".

Read the first line from the inside to the outside:

OrderCodeWhat happens
1IO.readln("Umur: ")Waits for typed text. The result is the string "17"
2Integer.parseInt(...)The text necklace "17" is turned into the number 17
3int umur = ...The number 17 goes into an int box

Now you can calculate with umur. umur + 1 is 18.


5. The Classic Funnel: Scanner

In many books and courses, you will meet an older but very popular funnel: Scanner.

JAVA
void main() {
    Scanner pindai = new Scanner(System.in);

    IO.print("Umur: ");
    int umur = pindai.nextInt();
    IO.println("Tahun depan umurmu " + (umur + 1));
}
TEXT
Umur: 17
Tahun depan umurmu 18

The first line creates a Scanner that reads from the keyboard. I named it pindai, which means "scan". You do not need to think about the word new yet. We cover it in the part about classes and objects.

CodeIts job
pindai.nextInt()Reads one whole number
pindai.nextLine()Reads one line of text

For a one-file program like this, Java 25 lets you use Scanner directly, without an import line. In other tutorials, you may see import java.util.Scanner; on the first line.

So when you see Scanner somewhere else, you now know what it is: a funnel with the same job.


6. A Real Example: Guess the Birth Year

Create a file named Lahir.java. Lahir means "born".

JAVA
void main() {
    String nama = IO.readln("Nama: ");
    int umur = Integer.parseInt(IO.readln("Umur: "));
    int lahir = 2026 - umur;

    IO.println(nama + " lahir sekitar tahun " + lahir);
}
TEXT
Nama: Rani
Umur: 17
Rani lahir sekitar tahun 2009

The last line says "Rani was born around the year 2009". The word "around" matters. Our program does not know yet if Rani has already had her birthday this year.

The number 2026 is the year when I wrote this article. Change it to the year when you read it.


7. Three Input Traps

Trap one: forgetting that input is a string.

JAVA
void main() {
    String angka = IO.readln("Angka: ");
    IO.println(angka + 1);
}
TEXT
Angka: 5
51

The user types 5, we add 1, but the result is not 6. Remember Part 4: as soon as there is a string, the + sign joins.

Trap two: parseInt gets text that is not a number.

If the user types the age in words, such as tujuh belas ("seventeen"), the program stops:

TEXT
Umur: tujuh belas
Exception in thread "main" java.lang.NumberFormatException: For input string: "tujuh belas"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
	at java.base/java.lang.Integer.parseInt(Integer.java:565)
	at java.base/java.lang.Integer.parseInt(Integer.java:662)
	at Umur.main(Umur.java:2)

The message is long, but you only need to read two lines. The first line names the problem: NumberFormatException, the text is not a number. The last line gives the address: Umur.java, line 2. We learn how to handle this in Part 18.

Trap three, only for Scanner: nextInt() followed by nextLine().

JAVA
void main() {
    Scanner pindai = new Scanner(System.in);

    IO.print("Umur: ");
    int umur = pindai.nextInt();
    IO.print("Nama: ");
    String nama = pindai.nextLine();

    IO.println("Nama: [" + nama + "], umur: " + umur);
}
TEXT
Umur: 17
Nama: Nama: [], umur: 17

The program does not wait for the name. Why?

nextInt() takes only the number. The Enter key is left in the funnel. The next nextLine() picks up that Enter at once, so the name is read as empty.

The fix: call pindai.nextLine(); one time right after nextInt(), to throw away the leftover Enter.


Summary

  1. Input is a funnel. IO.readln waits for the user to type, then gives the result as a string.
  2. For a number, convert that string with Integer.parseInt.
  3. Scanner is the classic funnel. Watch out for the leftover Enter after nextInt().

Challenge

Look at this code:

JAVA
void main() {
    int a = Integer.parseInt(IO.readln());
    String b = IO.readln();
    IO.println(a + 1 + b);
}

The user types 7, then 3. What is the output?

  • A. 11
  • B. 83
  • C. 713

Try to answer before you run it. I explain the answer at the start of Part 7.


Conclusion & What's Next

Your program can now listen. If you remember only one thing, remember that readln always gives a string. A number must be converted first with parseInt.

In Part 7, our program starts to choose a path. There is a road junction with a sign, and a lift with floor buttons.

See you in Part 7. Get your coffee ready. Hot chocolate is fine too.

ShareLinkedInXWhatsApp

Written by Muhammad Fari Madyan

Senior Backend Engineer and co-founder of LampungDev. I write up what I build so others can learn from it.

Work with me
← part 5Operatorspart 7 →if, else and switch

Muhammad Fari Madyan

Senior Backend Engineer building real-time, high-concurrency systems that hold up in production. Co-founder of LampungDev. Based in Bandar Lampung, Indonesia (UTC+7), working remote.

Work

Case studiesCahTixExam platform, 4 stacksOpen source

© 2026 Muhammad Fari Madyan

Designed and built by Muhammad Fari Madyan