Friday, 22 May 2020

how to check url is valid or not in java







import java.net.URL;

import java.util.Scanner; 

 

class URLTest {

 

   //static method to validate URL

    public static boolean isValid(String url){

try{

new URL(url).toURI();

return true;

}catch(Exception e){

//if exception generate than return false

return false;

}

}

    public static void main(String[] args)

    {

// user URL input

System.out.print("Enter URL: ");

Scanner sc = new Scanner(System.in);

String url = sc.nextLine();



//now check by method

if(isValid(url)){

System.out.print("\nYes URL is Valid ");

}else{

System.out.print("\n No  URL is Not Valid ");

}



    }

}

No comments:

Post a Comment

JTable Hide and Show in Java Swing