Java Array initialize arrays with input values

Introduction

The following loop initializes the array array with user input values.


import java.util.Arrays;

public class Main {
  public static void main(String[] args) {
    int[] array = new int[5];

    java.util.Scanner input = new java.util.Scanner(System.in);
    System.out.print("Enter " + array.length + " values: ");
    for (int i = 0; i < array.length; i++)
      array[i] = input.nextInt();//  www.j a  va  2  s .c  om
    
    System.out.println(Arrays.toString(array));

  }
}



PreviousNext

Related