maygapixel

Untitled

Apr 28th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Scanner;
  2. import java.io.File;
  3. import java.util.InputMismatchException;
  4. import java.io.FileNotFoundException;
  5. public class Exception2 {
  6.   public static void main(String[] args) {
  7.     int total = 0;
  8.     int num = 0;
  9.     File myFile = null;
  10.     Scanner inputFile = null;
  11.    try{
  12.     myFile = new File("inFile.txt");
  13.     inputFile = new Scanner(myFile);
  14.     while (inputFile.hasNext())  {
  15.       try {
  16.        num = inputFile.nextInt();
  17.        total += num;
  18.       }
  19.       catch (InputMismatchException ime) {
  20.            System.out.print("Illegal value found\n");
  21.            inputFile.next();
  22.       }
  23.    }
  24.     System.out.println("The total value is " + total);
  25.   }
  26.   catch (FileNotFoundException fnf){
  27.   System.out.println("The total value is " + total);  
  28.   System.out.println(fnf.getMessage());
  29.   }
  30.  }
  31. }
Add Comment
Please, Sign In to add comment