Slip 28 - A) Write a java program to count the number of integers from a given list. (Use Command line arguments).

Solution :

import java.util.*;

public class Slip28A {

    public static void main(String[] args) {

        int count = 0;

        List<String> al = new ArrayList<>();

        for (int i = 0; i < args.length; i++) {

            al.add(args[i]);

        }

        for (int i = 0; i < al.size(); i++) {

            String element = al.get(i);

            try {

                int j = Integer.parseInt(element);

                count++;

            } catch (NumberFormatException e) {}

        }

        System.out.println(count + " integers present in list");

    }

}

Output:

Output_pic


Post a Comment

0 Comments