Slip No 28 Q A

Q.Write a java program to count the number of integers from a given list. (Use Command line arguments). [15 M]

public class CountIntegers
{
    public static void main(String args[])
    {
        int count = 0;

        for(int i = 0; i < args.length; i++)
        {
            try
            {
                Integer.parseInt(args[i]);   // check if integer
                count++;
            }
            catch(NumberFormatException e)
            {
                // ignore non-integer values
            }
        }

        System.out.println("Number of integers = " + count);
    }
}
Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top