java - Exception handling - sum of numebrs - NumberFormatException


/*
Write a java program for exception handling:
To add two integers and raise exception when any other character except number (0 – 9) is given as input.
*/

// Java Exception Handling
// Number format exception

import java.util.*;

class numberformatexceptions
{
     public static void main(String args[])
     {
          String s1, s2;
          Scanner s = new Scanner(System.in);
         
          try
          {
              System.out.print("Enter first number : ");
              s1 = s.next();
              System.out.print("Enter second number : ");
              s2 = s.next();
             
              int x = Integer.parseInt(s1);
              int y = Integer.parseInt(s2);
             
              System.out.print("Sum = "+(x+y));
             
          }
          catch(Exception e)
          {
              System.out.println(e) ;
          }
     }
}

No comments:

Post a Comment

Don't be a silent reader...
Leave your comments...

Anu