Sunday, 12 March 2023

6. Write a program that takes a string from the user and validate it. The string should be at least 5 characters and should contain at least one digit. Display an appropriate valid message.

 


import java.util.*;

class ValidateString extends Exception
{
    ValidateString(String s)
    {
        System.out.println("\nString is not valid");
    }
}

class Program6
{
    public static void main(String args[])
    {
        System.out.print("enter a String under 5 Characters and
one character is number: ");
        Scanner sc = new Scanner(System.in);
        String str = sc.next();
       
        char arr[] = str.toCharArray();
       
        try{
        int flag = 0;
        if(str.length() == 5)
        {
            for(int i=0; i<5; i++)
            {
                if(Character.isDigit(arr[i]))
                {
                    flag = 1;
                }
            }
            if(flag == 1)
                System.out.println("String is valid");
            else
                throw new ValidateString(str);
        }
        else
            throw new ValidateString(str);
        }
        catch(Exception e)
        { }
    }
}


No comments:

Post a Comment

python programs

1. sum of two number