• 0

[JAVA] MalformedURLException


Question

import java.net.*;
import java.io.*;

public class Arachnid{
    public static String[] aWeb = {"www.neowin.net", "www.planethalflife.com"};
    
    public static void main(String[] args) {
        for (int i = 0; i<aWeb.length; i++) {
            System.out.println(aWeb[i]);
            Spider spider = new Spider(new URL(aWeb[i])); // <------  MALFORMEDURL OCCURS HERE
        }
    }  
}


/* the constructor i am using, my problem doesnt currently exist with this*/
class Spider extends Thread {
    public URL web;
// ....
    public Spider(URL w) {
        web = w;
    }
}

i have tried replacing aWeb with each individual address and the same error occurs

i have also tried outputting the values of aWeb to console

any help or input would be appreciated

thanks in advance

Link to comment
Share on other sites

7 answers to this question

Recommended Posts

  • 0

Only thing I can think of is including the protocol in the URLs, so it becomes:

public static String[] aWeb = {"https://www.neowin.net", "http://www.planethalflife.com"};

Link to comment
Share on other sites

  • 0
Only thing I can think of is including the protocol in the URLs, so it becomes:

public static String[] aWeb = {"https://www.neowin.net", "http://www.planethalflife.com"};

yeh i have :happy:

this is really weird aye, ive written something VERY similar before, and it worked, but this time :pinch:

Link to comment
Share on other sites

  • 0

Hum, I copied/pasted your code, and tried to compile it... It seems that you forgot to catch the MalformedUrlException. Change your code into this :

import java.net.*;
import java.io.*;

public class Arachnid{
   public static String[] aWeb = {"https://www.neowin.net", "http://www.planethalflife.com"};
   
   public static void main(String[] args) {
       for (int i = 0; i<aWeb.length; i++) {
           System.out.println(aWeb[i]);

           try {
   
               Spider spider = new Spider(new URL(aWeb[i])); // <------  MALFORMEDURL OCCURS HERE

            } catch(MalformedURLException mue) {

               System.err.println(mue.getMessage());

            }

       }
   }  
}


/* the constructor i am using, my problem doesnt currently exist with this*/
class Spider extends Thread {
   public URL web;
// ....
   public Spider(URL w) {
       web = w;
   }
}

And do not forget to add "http://" in front of your Urls :) It worked for me :cool:

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.