the date.txt file contains numbers form 1-100, I want to sum and copy them in arr[] but I get noSuchElementException ... Is it why, after counting the lines, the pointers goes to 100th line and then starts form there again??? I have reset the point though ...Code:File f = new File("F:/" + date + ".txt");
if (!f.exists()) {
System.out.println("file not found !");
System.exit(1);
}
int lines = 0;
@SuppressWarnings("resource")
Scanner scan = new Scanner(f);
while (scan.hasNext()) {
System.out.println(scan.nextLine());
lines++;
}
System.out.println(lines);
scan.reset();
int sum = 0;
int arr[] = new int[lines];
for (int i = 0; i < lines; i++) {
arr[i] = Integer.valueOf(scan.nextLine()).intValue();
sum += arr[i];
}
System.out.println(sum);