DEV Community

Discussion on: Daily Challenge #178 - Create Phone Numbers

Collapse
mathanagopal97 profile image
Mathanagopal Sankarasubramanian

JAVA

    public static String getNumbers(int[] arr, int start, int end){
        String number = "";
        for(int i=start;i<=end;i++){
            if((""+arr[i]).length()>=2) {
                System.out.println("Let all the numbers be of single digit (Number at position "+i+").");
                System.exit(1);
            }
            number = number+arr[i];
        }
        return number;
    }

    public static String generateNumbers(int[] arr){
        if(arr.length<0) System.out.println("Not valid");
        else{
            String phoneNumber = "("+getNumbers(arr,0,2)+")"+getNumbers(arr,3,5)+"-"+getNumbers(arr,6,arr.length-1);
            return phoneNumber;
        }
        return "";
    }
    public static void main(String[] args) {
        int[] arr = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
        System.out.println(generateNumbers(arr));
    }