Why does my Java array keep showing 0 values after using a for loop and Scanner?
21:51 25 May 2026

I am learning arrays and loops in Java.

I am trying to store numbers entered by the user into an array using Scanner, but when I print the array, all values appear as 0.

This is my code:

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int[] numbers = new int[5];

        for(int i = 0; i < numbers.length; i++) {
            int value = input.nextInt();
        }

        for(int i = 0; i < numbers.length; i++) {
            System.out.println(numbers[i]);
        }
    }
}

Input used:

5
10
15
20
25

Output:

0
0
0
0
0

What I expected:
I expected the array to store and print the numbers entered by the user.

What I already tried:

  • Checking the loop conditions

  • Printing the values individually

Why are all array values still 0 after entering the numbers?

java arrays println