Java Program Count invalid Mobile numbers form list

An string or character array containing five Mobile numbers in provided.

Verify these five mobile numbers and identify valid as well as invalid numbers.

Any mobile number less than or greater than 10 digits or any character is considered as invalid.

Write a code which will generate report on count of total number of invalid mobile numbers.

A valid Mobile number ‘M’ should be digit of length 10 with all numeric digits.

Do not use any input message. Directly accpet Mobile Numbers one after other as input enter “q” or “Q” to stop taking input.

Example:

9892568790
989A2568790
9992568790
99925687999
989256879B Q

Output:

3

Main.java

import java.util.ArrayList;
import java.util.Scanner;

public class mobile_number {
    public static boolean containOnlyNumber(String str){
        for (int i=0;i<str.length();i++){
            if(!Character.isDigit(str.charAt(i)))
                return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count =0;;

        ArrayList<String>  myList = new ArrayList<String>();
        while (true){
            String str = sc.nextLine();
            if(str.equalsIgnoreCase("q")){
                break;
            }
            else {
                myList.add(str);
                if(str.length() != 10 || containOnlyNumber(str)==false){
                    count++;
                }
            }
        }
        System.out.println(count);
    }
}

Recommended:

Leave a Comment

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