I'm an amateur with Java and am trying to put the contents of a scanner into an array. This is what I have currently: `
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int l = scannerLength(sc);
sc.close(); sc = null; sc = new Scanner(System.in);
int[] input = new int[l];
for (int i = 0; i < l; i++) {
input[i] = sc.nextInt();
System.out.println(input[i]);
}
}
private static int scannerLength(Scanner sc) {
int output = 0;
while(sc.hasNextInt()) {
output++;
sc.nextInt();
}
return output;
}
`
I am trying to effectively 'reset' the Scanner after my helper function determines its length by voiding it and declaring it anew, so I can use nextInt to put the elements into an array. But I get a NoSuchElementException at line 8.
Can anyone tell me why this is? I would think, after resetting the Scanner, that sc.nextInt() at line 8 wouldn't pose a problem.