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());
       }
    }
}

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

commented: why not :-) +9

Thank you for your help. The above solution worked.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.