Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default casting from String to URL

    Hello,

    I try to convert String to ULR , but I cannot cast the string to URL, How could i do it ?
    I shoult too to use the "throws MalformedURLException"

    I try to do this :

    PHP Code:
    String webSite;
    URL url;
    if(url.equals(webSite))
    (URL) webSite
    Can Anyone help me ?

    Thank u

  2. #2
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    Use the URL(String) constructor to make a new URL.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  3. #3
    makpandian's Avatar
    makpandian is offline Senior Member
    Join Date
    Dec 2008
    Location
    Chennai
    Posts
    448
    Rep Power
    6

    Default

    Every URL should has protocol name,host name ,port number.
    So make your url in proper format
    Mak
    (Living @ Virtual World)

  4. #4
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    Thank you,

    Is this code cast the URL to String ?

    PHP Code:
    	URL url = new URL(webSite);
    		
    		try {
    	        // Get the image
    	        InputStream is = url.openStream();
    	        is.close();
    	    } catch (MalformedURLException e) {
    	        // Print out the exception that occurred
    	        System.out.println("Invalid URL "+webSite+": "+e.getMessage());
    	    } catch (IOException e) {
    	        // Print out the exception that occurred
    	        System.out.println("Unable to execute "+webSite+": "+e.getMessage());
    	    }

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by alrebatsd View Post
    Thank you,

    Is this code cast the URL to String ?

    PHP Code:
    	URL url = new URL(webSite);
    You're not casting a string to a URL there, you are instantiating a URL object, using a constructor that takes a String parameter. They're different things.

  6. #6
    alrebatsd is offline Member
    Join Date
    Apr 2009
    Posts
    28
    Rep Power
    0

    Default

    I didn't understood u,

    Now I have a String and I want to cast it to URL.

    I try this code:
    PHP Code:
    public static void main(String[] args) throws MalformedURLException {
    		// TODO Auto-generated method stub
    		enumTest e;
    		e=(enumTest)enumTest.valueOf(c);
    		System.out.println(e);
    
    		String webSite ="htp://java-forums.org";
    		URL ur = new URL(webSite);
    		try {
    	        InputStream is = ur.openStream();
    	        is.close();
    	    } catch (MalformedURLException m) {
    	        // Print out the exception that occurred
    	        System.out.println("Invalid URL "+webSite+": "+m.getMessage());
    	    } catch (IOException m) {
    	        // Print out the exception that occurred
    	        System.out.println("Unable to execute "+webSite+": "+m.getMessage());
    	    }
    	    finally{
    	    System.out.println(ur);
    	    }
    
    		
    	}
    }
    and the outptut was:
    PHP Code:
    htp://java-forums.org
    "it should with http but I cannot add links"

    that's meaning that it cast the String webSite to URL because I try to print the URL ur !!

    is it right ?!

  7. #7
    OrangeDog's Avatar
    OrangeDog is offline Senior Member
    Join Date
    Jan 2009
    Location
    Cambridge, UK
    Posts
    838
    Rep Power
    6

    Default

    That's not a cast. That's making a new URL by passing its constructor a String. When you print it out, it's calling the toString() method, which returns a String. A cast doesn't make new objects, it just changes the declaration. URLs and Strings are incompatible for casting.
    Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
    How To Ask Questions The Smart Way

  8. #8
    jiapei100 is offline Member
    Join Date
    Aug 2009
    Location
    Richmond, Canada
    Posts
    15
    Rep Power
    0

    Default

    Very good. You give a very clear explanation, but no solution at all.
    Very good..

    Any other guys have the solution, please post. Thanks.

    Rgds
    JIA

    Quote Originally Posted by OrangeDog View Post
    That's not a cast. That's making a new URL by passing its constructor a String. When you print it out, it's calling the toString() method, which returns a String. A cast doesn't make new objects, it just changes the declaration. URLs and Strings are incompatible for casting.
    Welcome to Vision Open
    http://www.visionopen.com

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The answer is: you don't create a URL by casting a String as it just can't be done, which I believe was explained above. If this doesn't answer a specific question that you have, I invite you to start a new thread and please feel free ask it. Oh, and welcome to the forum!

  10. #10
    jiapei100 is offline Member
    Join Date
    Aug 2009
    Location
    Richmond, Canada
    Posts
    15
    Rep Power
    0

    Default

    Haha, thank you ...

    Sorry for my attitude because I really need the answer.

    I found a way, too old way.

    That is
    new File ("the directory").toURL();

    but, it seems toURL() is deprecated now.

    if I do
    URL aaa = new URL("the directory");
    it's still wrong.

    What I mean is can you show me your code as the solution?
    Welcome to Vision Open
    http://www.visionopen.com

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    What do you mean by "it's still wrong"? Does it compile and throw a run-time exception? Does it not compile and show a compiler error? Any errors or exceptions should be posted here in full if possible.

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

    Default

    See #3.

    db

  13. #13
    jiapei100 is offline Member
    Join Date
    Aug 2009
    Location
    Richmond, Canada
    Posts
    15
    Rep Power
    0

    Default

    Ok, why not just show your code directly? Things will be much clearer with one line of code.

    So, even if without the code, can you please tell me if I just want to getResource directly from a local file on my driver as a URL, what should be the protocol name, what should be the host name, what is the port number?

    Following the definition here in URL.class
    public URL(String protocol, String host, String file)

    I tried
    filename = new URL(null, null, "full address of the filename");
    no compilation error but run-time error

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.net.URL.<init>(URL.java:358)
    at java.net.URL.<init>(URL.java:283)
    at java.net.URL.<init>(URL.java:306)
    at org.jdesktop.j3d.examples.print_canvas3d.PrintCanv as3D.<init>(PrintCanvas3D.java:240)
    at org.jdesktop.j3d.examples.print_canvas3d.PrintCanv as3D$4.run(PrintCanvas3D.java:378)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)



    Did you really ever run such kind of thing without posting an even one line of code?

    Cheers
    hehe



    Quote Originally Posted by makpandian View Post
    Every URL should has protocol name,host name ,port number.
    So make your url in proper format
    Welcome to Vision Open
    http://www.visionopen.com

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by jiapei100 View Post
    Ok, why not just show your code directly? Things will be much clearer with one line of code.

    So, even if without the code, can you please tell me if I just want to getResource directly from a local file on my driver as a URL, what should be the protocol name, what should be the host name, what is the port number?

    Following the definition here in URL.class
    public URL(String protocol, String host, String file)

    I tried
    filename = new URL(null, null, "full address of the filename");
    no compilation error but run-time error
    OK, your protocol there is "file". The host is (I think, though I've not tried it) a blank string ""...the "full address" is the location of your file.

    This is based entirely on what you've provided here, though I strongly suspect you're going about this the wrong way.

  15. #15
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    If you want to access a file, the protocol is "file:", if it's on your local machine the host will be "localhost" or "127.0.0.1".

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by dlorde View Post
    If you want to access a file, the protocol is "file:", if it's on your local machine the host will be "localhost" or "127.0.0.1".
    I'm pretty sure the host is an empty string. And you don't need the ":".
    A little test and this works:

    Java Code:
    try {
    	URL url = new URL("file","","/test/test.txt");
    	File f = new File(url.toURI());
    	BufferedReader br = new BufferedReader(new FileReader(f));
    	System.out.println(br.readLine());
    }
    finally { br.close(); }
    (You'll have to imagine the various catches).

    Unless it's the way I've written it, of course. Not had to use it to get hold of files on the machine before.

  17. #17
    jiapei100 is offline Member
    Join Date
    Aug 2009
    Location
    Richmond, Canada
    Posts
    15
    Rep Power
    0

    Default

    Thanks !! Tolls.
    This works for me.
    I tried
    Java Code:
    URL url = new URL("file","127.0.0.1","/test/test.txt");
    which seems to be wrong. So, leave it blank seems to be the right way out.

    Cheers


    Quote Originally Posted by Tolls View Post
    I'm pretty sure the host is an empty string. And you don't need the ":".
    A little test and this works:

    Java Code:
    try {
    	URL url = new URL("file","","/test/test.txt");
    	File f = new File(url.toURI());
    	BufferedReader br = new BufferedReader(new FileReader(f));
    	System.out.println(br.readLine());
    }
    finally { br.close(); }
    (You'll have to imagine the various catches).

    Unless it's the way I've written it, of course. Not had to use it to get hold of files on the machine before.
    Welcome to Vision Open
    http://www.visionopen.com

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by jiapei100 View Post
    Thanks !! Tolls.
    This works for me.
    I tried
    Java Code:
    URL url = new URL("file","127.0.0.1","/test/test.txt");
    which seems to be wrong. So, leave it blank seems to be the right way out.

    Cheers
    Well, that's something.
    Note, I'm only going on blank for the host based on IE using the format "file:///file path goes here", so assuming the host would normally fit in after the second "/".

    I haven't used this, or written anything that uses a URL object referencing a file, so buyer beware.

  19. #19
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Tolls View Post
    I'm pretty sure the host is an empty string. And you don't need the ":".
    You're right - I was careless - although "localhost" works here.

    I don't think the intermediate File conversion is necessary, you should be able to get a stream directly from the URL:
    Java Code:
    BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
    Last edited by dlorde; 08-24-2009 at 04:49 PM. Reason: addition

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Quote Originally Posted by dlorde View Post
    You're right - I was careless - although "localhost" works here.

    I don't think the intermediate File conversion is necessary, you should be able to get a stream directly from the URL:
    Java Code:
    BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
    Hah!
    Good point!
    That's what comes from writing my test bit into an existing file reading chunk of code...:)
    Rather than rewrite properly, I did a "how do I get this to work with that" thing...and wrote something slightly odd.

Page 1 of 2 12 LastLast

Similar Threads

  1. What does casting mean?
    By sev51 in forum New To Java
    Replies: 3
    Last Post: 01-27-2009, 04:31 PM
  2. casting help
    By soc86 in forum New To Java
    Replies: 4
    Last Post: 01-13-2009, 11:07 PM
  3. Object to Int casting
    By nn12 in forum New To Java
    Replies: 4
    Last Post: 12-06-2008, 10:58 PM
  4. class casting
    By vital parsley in forum New To Java
    Replies: 1
    Last Post: 07-24-2008, 10:03 AM
  5. Casting
    By leebee in forum New To Java
    Replies: 5
    Last Post: 08-10-2007, 12:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •