How can I store user inputs in an array of integers?

htt‮sp‬://www.lautturi.com
How can I store user inputs in an array of integers?

To store user inputs in an array of integers in Java, you can use a loop to read the inputs and add them to the array.

Here's an example of how to do it:

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    // Create a Scanner object for reading input
    Scanner scanner = new Scanner(System.in);

    // Create an array of integers
    int[] array = new int[5];

    // Read the user inputs and add them to the array
    for (int i = 0; i < array.length; i++) {
      System.out.print("Enter a number: ");
      array[i] = scanner.nextInt();
    }

    // Close the Scanner
    scanner.close();
  }
}

This code will read 5 integers from the user and store them in the array array. You can adjust the size of the array and the number of inputs accordingly.

Created Time:2017-11-01 12:04:58  Author:lautturi