We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Java
  3. Exception Handling
  4. Java Exception Handling (Try-catch)
  5. Discussions

Java Exception Handling (Try-catch)

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • jhonjairomoreno1
    2 months ago+ 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner input = new Scanner(System.in);
            int x;
            int y;
            
            try{
                x = input.nextInt();
                y = input.nextInt();
                float resp = x/y;
                if(resp%10==0){
                    System.out.println(Math.round(resp));
                }else{
                    System.out.println(y);
                }
            
            }catch(InputMismatchException e){
                System.out.println(e.getClass().toString().substring(6));
            }catch(ArithmeticException a){
                System.out.println(a);
            }
        }
    }
    
    0|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy