Java Blog

 
 
 
class ExcSort
{
       public static void sort(int arr[]) throws ArithmeticException,
       ArrayIndexOutOfBoundsException, NullPointerException
       {
              int rem=0;
              int sum_dig_arr[]={0, 0, 0, 0, 0};
              System.out.println(\"\\n\"+\"The Summed Array:\");
              for(int i=0; i<5; i++)
              {
                     while(arr[i]!=0)
                     {
                         rem=arr[i]%10;
                         sum_dig_arr[i]=sum_dig_arr[i]+rem;
                         arr[i]=arr[i]/10;
                     }
                     System.out.println(sum_dig_arr[i]);
              }
              System.out.println(\"\\n\"+\"The Sorted Array:\");
              for(int i=0; i<5; i++)
              {
                   for(int j=i+1; j<5; j++)
                   {
                        if(sum_dig_arr[j] < sum_dig_arr[i])
                        {
                              int temp=sum_dig_arr[i];
                              sum_dig_arr[i]=sum_dig_arr[j];
                              sum_dig_arr[j]=temp;
                        }
                   }
                   System.out.println(sum_dig_arr[i]);
              }
             // throw new ArithmeticException(\"Caught...\");
             // throw new ArrayIndexOutOfBoundsException(\"Caught...\");
             // throw new NullPointerException(\"Caught...\");
       } 

       public static void main(String args[])
       {
              int array[]=new int[5];
              for(int i=0; i<5; i++)
              {
                     array[i]=Integer.parseInt(args[i]);
              }
              try
              {
                     sort(array);
              }
              catch(ArithmeticException ae)  
              {
                      System.out.println(\"Caught \"+ae);
              }       
              catch(ArrayIndexOutOfBoundsException ie)
              {
                      System.out.println(\"Caught \"+ie);
              }       
              catch(NullPointerException ne)
              {
                      System.out.println(\"Caught \"+ne);
              }       
       }    
}


/*
            Output

java ExcSort 34 2 45 21 5

The Summed Array:
7
2
9
3
5

The Sorted Array:
2
3
5
7
9

*/