Skip to content

Instantly share code, notes, and snippets.

@airicbear
Created May 25, 2020 03:44
Show Gist options
  • Save airicbear/75b4f98695f0138eebed3d8483798754 to your computer and use it in GitHub Desktop.
Save airicbear/75b4f98695f0138eebed3d8483798754 to your computer and use it in GitHub Desktop.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class SumOfIntegers {
public static void main(String[] args) {
String pathToFile = args[0];
File file = new File(pathToFile);
int sum = 0;
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextInt()) {
sum += scanner.nextInt();
}
} catch (FileNotFoundException e) {
System.out.println("No file found: " + pathToFile);
}
System.out.println(sum);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment