Tuesday, 4 March 2025

Write Java program which accepts string from user, if its length is less than five, then throw user defined exception “Invalid String” otherwise display string in uppercase.


import java.util.Scanner;


class InvalidStringException extends Exception

{

    InvalidStringException(String msg)

    {

        super(msg);

    }

}


class StringCheck

{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);


        try

        {

            System.out.print("Enter a string: ");

            String str = sc.nextLine();


            if(str.length() < 5)

            {

                throw new InvalidStringException("Invalid String");

            }


            System.out.println("Uppercase String: " + str.toUpperCase());

        }

        catch(Exception e)

        {

            System.out.println("Exception: " + e.getMessage());

        }


        sc.close();

    }


}