I have this code to shift the elements of array since i was taking array input and printing them i created a java file for taking input and printing them code for that--
import java.util.*;
public class test_inp_out_array {
public int[] inputArray(int n) {
Scanner sc = new Scanner(System.in);
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
sc.close();
return arr;
}
public void printArray(int arr[]) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
}
and i am using it in different files as methods in the --
import java.util.*;
public class cn_46_rotate_array {
public static int[] shift(int arr[], int d) {//method to shift the elements
int temp[] = new int[d];
for (int j = 0; j < d; j++) {
temp[j] = arr[j];
}
for (int i = 0, j = 0; i < arr.length && j < d; i++) {
if (i < arr.length - d) {
arr[i] = arr[i + d];
} else {
arr[i] = temp[j];
j++;
}
}
return arr;
}
public static void main(String[] args) {
Scanner ab = new Scanner(System.in);
test_inp_out_array ch = new test_inp_out_array();
int n = ab.nextInt();//total number of elements in array
int d = ab.nextInt();//difference by which i want to shift elements in array
int arr[] = ch.inputArray(n);
int arr1[] = shift(arr, d);
ch.printArray(arr1);
ab.close();
}
}
the program works fine when written in this way but it throws this error when i take input of variable 'd' after calling method 'ch.inputArray'
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.Scanner.throwFor(Scanner.java:937)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
at cn_46_rotate_array.main(cn_46_rotate_array.java:25)
the reason behind this is that i am closing scanner object 'sc' in my test_inp_array.java file when i call the method 'inputArray' which is also closing the input process in the file where im calling it so what should i do to avoid resource leak and take input of variable 'd' after storing the elements in array. what can be the other solution other than surrounding 'sc' object with try-with-resources in java.