Skip to content

Instantly share code, notes, and snippets.

@roopeshsn
Created October 28, 2022 11:41
Show Gist options
  • Save roopeshsn/0daf2610c9ebe8fd0d7d940363a03362 to your computer and use it in GitHub Desktop.
Save roopeshsn/0daf2610c9ebe8fd0d7d940363a03362 to your computer and use it in GitHub Desktop.
Code to get array input from command-line in Java
package com.roopesh;
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = getIntegerArray();
System.out.println(Arrays.toString(arr));
}
public static int[] getIntegerArray() {
Scanner scanner = new Scanner(System.in);
int size = scanner.nextInt();
int[] arr = new int[size];
for (int i = 0; i < size; ++i) {
arr[i] = scanner.nextInt();
}
return arr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment