Saturday, 23 May 2020

forEach Loop in Single Line of Code using JAVA 8 Feature



import java.util.List;

import java.util.ArrayList;



public class ForeachTest {



   public static void main(String args[]) {

    // create List

List<String> subjects = new ArrayList<String>();

subjects.add("C");

subjects.add("C++");

subjects.add("JAVA");

subjects.add(".NET");



//apply forEach in single line of code

subjects.forEach(System.out::println);

 

   }

}


















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 ");

}



    }

}

JTable Hide and Show in Java Swing