Niloy007

AssignmentOFSayem

May 16th, 2020
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.28 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.Scanner;
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5.  
  6. class ExplicitException extends Exception {
  7.     ExplicitException(double x) {
  8.         super("ExplicitException occured due to value: " + x);
  9.     }
  10. }
  11.  
  12.  
  13. public class Assignment {
  14.  
  15.     static void checkResult(double num) throws ExplicitException {
  16.         if(num < 5) {
  17.             throw new ExplicitException(num);
  18.         } else {
  19.             try {
  20.                 BufferedWriter writer = new BufferedWriter(new FileWriter("/Users/niloykumarkundu/Desktop/Assignment/src/output.txt"));
  21.                 writer.write("" + num);
  22.                 writer.close();
  23.             } catch (FileNotFoundException e) {
  24.                 e.printStackTrace();
  25.             } catch (IOException e) {
  26.                 e.printStackTrace();
  27.             }
  28.         }
  29.     }
  30.  
  31.  
  32.     public static void main(String[] args) {
  33.         try {
  34.             BufferedReader bufferedReader = new BufferedReader(new FileReader("/Users/niloykumarkundu/Desktop/Assignment/src/input.txt"));
  35.  
  36.             int a = 0, b = 0, c = 0;
  37.             double res = 0;
  38.             String input;
  39.  
  40.             try {
  41.                 while((input = bufferedReader.readLine()) != null) {
  42.                     String[] temp = input.split(" ");
  43.                     if(temp[0].equalsIgnoreCase("a")) {
  44.                         a = Integer.parseInt(temp[1]);
  45.                     } else if(temp[0].equalsIgnoreCase("b")) {
  46.                         b = Integer.parseInt(temp[1]);
  47.                     } else {
  48.                         c = Integer.parseInt(temp[1]);
  49.                     }
  50.                 }
  51.             } catch(Exception e) {
  52.                 System.out.println(e.getMessage());
  53.             }
  54.  
  55.             res = (a + b) / (double) c;
  56.             try {
  57.                 checkResult(res);
  58.             } catch (ExplicitException e) {
  59.                 System.out.println(e.getMessage());
  60.             }
  61.  
  62.         } catch (FileNotFoundException ex) {
  63.             ex.printStackTrace();
  64.         } catch (IOException e) {
  65.             e.printStackTrace();
  66.         } catch (NumberFormatException e) {
  67.             e.printStackTrace();
  68.         } catch (ArithmeticException e) {
  69.             e.printStackTrace();
  70.         }
  71.     }
  72. }
Add Comment
Please, Sign In to add comment