Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.Scanner;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- class ExplicitException extends Exception {
- ExplicitException(double x) {
- super("ExplicitException occured due to value: " + x);
- }
- }
- public class Assignment {
- static void checkResult(double num) throws ExplicitException {
- if(num < 5) {
- throw new ExplicitException(num);
- } else {
- try {
- BufferedWriter writer = new BufferedWriter(new FileWriter("/Users/niloykumarkundu/Desktop/Assignment/src/output.txt"));
- writer.write("" + num);
- writer.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static void main(String[] args) {
- try {
- BufferedReader bufferedReader = new BufferedReader(new FileReader("/Users/niloykumarkundu/Desktop/Assignment/src/input.txt"));
- int a = 0, b = 0, c = 0;
- double res = 0;
- String input;
- try {
- while((input = bufferedReader.readLine()) != null) {
- String[] temp = input.split(" ");
- if(temp[0].equalsIgnoreCase("a")) {
- a = Integer.parseInt(temp[1]);
- } else if(temp[0].equalsIgnoreCase("b")) {
- b = Integer.parseInt(temp[1]);
- } else {
- c = Integer.parseInt(temp[1]);
- }
- }
- } catch(Exception e) {
- System.out.println(e.getMessage());
- }
- res = (a + b) / (double) c;
- try {
- checkResult(res);
- } catch (ExplicitException e) {
- System.out.println(e.getMessage());
- }
- } catch (FileNotFoundException ex) {
- ex.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (NumberFormatException e) {
- e.printStackTrace();
- } catch (ArithmeticException e) {
- e.printStackTrace();
- }
- }
- }
Add Comment
Please, Sign In to add comment