0

I know this may be easy to many of you but it hasn't been so for me. I want to apply some exception handling in this simple code. My instructor skimmed over this. If I were in the world of visual studio I would have applied some isNumeric, and some try and catch divide by zero. Java seems to be a different animal altogether.
PLEASE DO NOT CRITICISE I AM TRYING TO LEARN.

import java.util.Scanner;

public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner input = new Scanner( System.in);
        
        int Num1, Num2=1, sum, product, difference;
       float division;
       
       try 
       {
        while(Num2 !=0)
        {
            System.out.print("Enter first Number: ");
            Num1 = input.nextInt();
            System.out.print(" Enter Second Number: ");
            Num2 = input.nextInt();

            sum = Num1 + Num2;
            product = Num1 * Num2;
            difference = Num1 - Num2;

            division = (float)Num1/(float)Num2;

            System.out.printf("Sum =%d\n", sum);
            System.out.printf("Product =%d\n", product);
            System.out.printf("The difference =%d\n", difference);
            System.out.printf("Divion is %.3f\n ", division);
        }
       } 
       catch(Exception e)
       {
           System.err.println("Error " + e.getMessage());
       }
    }
}

Edited by domifa621: n/a

2
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by domifa621
1

You can write your own isInteger method like (

public boolean isInteger( String input )
{
   try
   {
      Integer.parseInt( input );
      return true;
   }
   catch( Exception e)
   {
      return false;
   }
}

or you can use patterns, but that can be overkill

Votes + Comments
why not :-)
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.