Java Program to Get Array Input

This post shows you multiple approaches to get array input in Java.

1. Using a Static Array (Hardcoded Values)

This is the simplest way to initialize values directly in the array.

package com.javacodepoint.array.basic;
//Get Array Input Using a Static Array (Hardcoded Values)
public class StaticArrayInput {
	public static void main(String[] args) {
		// Initialize an array with fixed values
		int[] numbers = { 10, 20, 30, 40, 50 };

		// Print the array elements
		System.out.println("Array elements:");
		for (int num : numbers) {
			System.out.print(num + " ");
		}
	}
}

OUTPUT:

2. Using Scanner (User Input from Console)

This approach lets the user input array elements during program execution.

package com.javacodepoint.array.basic;

import java.util.Scanner;

// Get Array Input Using Scanner (User Input from Console)
public class ScannerArrayInput {
	public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Ask user for the size of the array
        System.out.print("Enter the size of the array: ");
        int size = scanner.nextInt();

        // Declare the array
        int[] numbers = new int[size];

        // Input array elements from user
        System.out.println("Enter " + size + " elements:");
        for (int i = 0; i < size; i++) {
            numbers[i] = scanner.nextInt();
        }

        // Print the array elements
        System.out.println("Array elements:");
        for (int num : numbers) {
            System.out.print(num + " ");
        }
    }
}

OUTPUT:

3. Using Command-Line Arguments

Array values can be provided as input when running the program from the command line.

package com.javacodepoint.array.basic;

// Get Array Input Using Command-Line Arguments
public class CommandLineArrayInput {
	public static void main(String[] args) {
		// Length of args represents the number of command-line inputs
		int[] numbers = new int[args.length];

		// Convert String inputs to integers
		for (int i = 0; i < args.length; i++) {
			numbers[i] = Integer.parseInt(args[i]);
		}

		// Print the array elements
		System.out.println("Array elements:");
		for (int num : numbers) {
			System.out.print(num + " ");
		}
	}
}

Command to Run:

java CommandLineArrayInput 5 10 15 20

OUTPUT:

4. Using Arrays Class and a Single Line Input

Here, you can take a single input line (space-separated) and convert it into an array.

package com.javacodepoint.array.basic;

import java.util.Arrays;
import java.util.Scanner;

//Get Array Input Using Arrays Class and a Single Line Input
public class ArraysClassInput {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		// Take a single line of input
		System.out.println("Enter array elements (space-separated):");
		String input = scanner.nextLine();

		// Convert input into an array
		int[] numbers = Arrays.stream(input.split(" ")).mapToInt(Integer::parseInt).toArray();

		// Print the array elements
		System.out.println("Array elements: " + Arrays.toString(numbers));
	}
}

OUTPUT:

5. Using Streams (Advanced)

You can use Java 8+ Streams to read and process array input efficiently.

package com.javacodepoint.array.basic;

import java.util.Scanner;
import java.util.stream.IntStream;

//Get Array Input Using Streams
public class StreamArrayInput {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);

		// Take input size
		System.out.print("Enter size of the array: ");
		int size = scanner.nextInt();

		// Input array elements using streams
		int[] numbers = IntStream.range(0, size).map(i -> {
			System.out.print("Enter element " + (i + 1) + ": ");
			return scanner.nextInt();
		}).toArray();

		// Print the array elements
		System.out.println("Array elements:");
		IntStream.of(numbers).forEach(num -> System.out.print(num + " "));
	}
}

OUTPUT:

Java logical programs list


Java Basic Programs

Java String Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *