I want to read and print the numbers from the Textfile (WLTP.txt) in the assets folder as an array, but the output of my code is everytime null
. I wonder why it is skipping the try part and jumping to the catch part.
public class WLTPc {
public static void main(String[] args) {
int [] data = readFiles("WLTP.txt");
System.out.println(Arrays.toString(data));
}
public static int [] readFiles(String file) {
try {
File f = new File(file);
Scanner s = new Scanner (f);
int ctr = 0;
while (s.hasNextInt()) {
ctr ++;
s.nextInt();
}
int[] arr = new int[ctr];
Scanner s1= new Scanner(f);
for (int i=0; i< arr.length; i++){
arr[i] = s1.nextInt();
}
System.out.println(Arrays.toString(arr));
return arr;
}
catch (Exception e){
return null;
}
}
}