Wednesday, April 2, 2014

Java program for even odd number using try catch block

import java.*;
class EvenOdd
{
public static void main(String[] args)
{
int a,i;
if(args.length>0)
{
for(i=0;i<args.length;i++)

{
try
{
a=Integer.parseInt(args[i]);
}
catch(NumberFormatException nfe)
{
System.out.print("\n\nThis is not a valid Integer,\nSo considering it as 0 \n\n");
a=0;
}
if(a%2==0)
{
System.out.print("\n"+a+"  ------->  Even\n");
}
else
{
System.out.print("\n"+a+"  ------->  Odd\n");

}
}
}
}
}

1 comment:

  1. class MyClass {
    static int odev(int a)
    throws IllegalArgumentException{

    if((a % 2!=0)){
    throw new IllegalArgumentException("This no should be even no");
    }
    else{
    return a + 0;
    }
    }
    }


    class OddEven{
    public static void main(String[]args){
    try
    {

    int x =MyClass.odev(10);

    System.out.println("x " + "is even");
    }
    catch (IllegalArgumentException e){
    System.out.println(e.getMessage());

    }
    }
    }

    ReplyDelete