Using URL me = new URL("http://www.google.com")

I've imported java.net, and I'm trying to open a connection to a website. When I try the following line:

URL me =new URL("http://www.google.com")

I get the compilation error:

java:157: unreported exception java.net.MalformedURLException; must be caught or declared to be thrown.

This is the onyl line in the function, so what could I be doing wrong?

Thanks for any help

[467 byte] By [Unconditional] at [2007-11-26 12:17:13]
# 1

First of all, theres no semi-colon after that line. Second of all, that constructor throws a MalformedURLException, which needs to be caught.

try

{

URL me = new URL("http://www.google.com");

}

catch(MalformedURLException e)

{

e.printStackTrace();

}

CaptainMorgan08 at 2007-7-7 14:54:23 > top of Java-index,Archived Forums,Socket Programming...
# 2

K thanks, another problem I'm having now is a compilation warning, I'm using Java 1.5, and using the following code:

InputStream is = null;

DataInputStream dis;

String s = "a";

try{

URL url = new URL(s1);

is = url.openStream();

dis = new DataInputStream(new BufferedInputStream(is));

while ((s = dis.readLine()) != null) {

System.out.println(s);

}

}

catch(Exception e){

}

I get the warning:

java:164: warning: [deprecation] readLine() in java.io.DataInputStream has been deprecated

while ((s = dis.readLine()) != null) {

^

1 warning

Any ideas?

Unconditional at 2007-7-7 14:54:23 > top of Java-index,Archived Forums,Socket Programming...
# 3
That method has been deprecated, dont use it.
CaptainMorgan08 at 2007-7-7 14:54:23 > top of Java-index,Archived Forums,Socket Programming...
# 4
Is there a suitable alternative I can use?
Unconditional at 2007-7-7 14:54:23 > top of Java-index,Archived Forums,Socket Programming...
# 5
Use a BufferedReader.readLine() instead. E.G. BufferedReader reader = new BufferedReader(new InputStreamReader(is),"utf-8");Note that you need to use the correct character encoding. I have only used "utf-8" as an example.
sabre150 at 2007-7-7 14:54:23 > top of Java-index,Archived Forums,Socket Programming...