See the code below, in this code I want to declare the Scanner class instance again and again for every input instance, I know I can declare the Scanner class instance outside the loop and the problem will be solved. But I am only showing you how I am implementing the same concept some where else. I cannot write that code here that's why I want you people top give me solution by understanding my perspective.
import java.util.Scanner;
public class TestScanner {
public static void main(String[] args) {
int a[] = new int[4];
System.out.println("Enter elements in array: ");
for (int i = 0; i < a.length; i++) {
Scanner scanner = new Scanner(System.in);
a[i] = scanner.nextInt();
scanner.close();
}
System.out.println("The Arrays is : ");
for (int i : a) {
System.out.println(i + " ");
}
}
}