import java.io.*; import java.util.*; public class TryWithResources { public static double addFileContents(String path) { File file = new File (path); try(Scanner sc = new Scanner(file)) { double sum = 0.0; while(sc.hasNextDouble()) { double value = sc.nextDouble(); sum += sc.nextDouble(); } } catch(FileNotFoundException ex) { System.out.println("The specified file was not found at "+ path); } catch(NoSuchElementException ex) { System.out.println("The specified type of element was not found!"); } return 0.0; } public static void main(String args[]) { try { double result = addFileContents(args[0]); System.out.println("Result is: "+result); } catch(IndexOutOfBoundsException ex) { System.out.println("No file has been specified from command line!\n"); } } }