Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cast a string to an URL in java?

Tags:

java

url

My java code is below.I wrote url=URL(s); but it is not true.I want to make a casting operation to convert a string that is taken from the user ,to an URL.How can I do this operation?Is there any method to do this?

    public static void main(String[] args) {
    System.out.println("Welcome to Download Manager");
    URL url;
    String s;
    Scanner scan= new Scanner(System.in);
    s=scan.nextLine();
    url=URL(s);
    Download download=new Download(url);
}
like image 351
ntf Avatar asked Apr 05 '13 19:04

ntf


2 Answers

You can't cast String to URL, since String is not a subclass of URL. You can create new instance of URL, passing String as argument to the constructor. In Java, you always invoke constructor using keyword new:

URL url = new URL(string);
like image 72
Grzegorz Olszewski Avatar answered Oct 08 '22 18:10

Grzegorz Olszewski


Using the URL constructor:

url = new URL(s);
like image 33
ngasull Avatar answered Oct 08 '22 17:10

ngasull