Advertisement
a1m

Untitled

a1m
Sep 8th, 2015
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.nio.file.Path;
  4. import java.nio.file.Paths;
  5. import java.util.Scanner;
  6.  
  7. public class Problem_08_SumNumbersFromTextFile {
  8.     public static void main(String[] args) {
  9.         boolean fileExist = true;
  10.         Scanner fileReader = null;
  11.         long sum = 0L;
  12.         Path currentRelativePath = Paths.get("");
  13.         String pathToFile = currentRelativePath.toAbsolutePath().toString();
  14.  
  15.         try {
  16.             File file = new File(pathToFile + "\\src\\input.txt");
  17.             fileReader = new Scanner(file);
  18.             int lineNumber = 0;
  19.             while (fileReader.hasNextLine()) {
  20.                 lineNumber++;
  21.                 sum += Long.parseLong(fileReader.nextLine());
  22.             }
  23.  
  24.         }
  25.  
  26.         catch (IOException IOexception){
  27.             fileExist = false;
  28.             System.out.println("error");
  29.         }
  30.  
  31.         if (fileExist) {
  32.             System.out.println(sum);
  33.             fileReader.close();
  34.         }
  35.  
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement