Skip to main content

Need help with code

1 reply [Last post]
Hodges
Offline
Joined: 2013-03-10
Points: 0

I'm currently learning Java with SAMS "Teach yourself Java in 21 days" and I've
hit a road block I'm unable to find a resolution. I'm implementing a 4 step
technique to open a connection to a WEB site and read an HTML doc from it but
the code compiles with errors that i can't seem to find. The declaration of
WindowListener 1 = new WindowAdapter() is where the errors begin in the NetBeans
IDE but it's exactly how the book wrote. Please help.

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

public class GetFile extends Frame implements Runnable {
Thread runner;
URL page;
TextArea box = new TextArea("Getting text...");

public GetFile() {
super("Get File");
add(box);
try {
page = new URL("http://quanhodges.com/Bio.html");
}
catch (MalformedURLException e) {
System.out.append("Bad URL: " + page);
}
}
public static void main(String[] arguments) {
GetFile frame = new GetFile();

WindowListener 1 = new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
};
frame.addWindowListener(1);
frame.setVisible(true);
if (frame.runner == null) {
frame.runner = new Thread(frame);
frame.runner.start();
}
}
public void run() {
URLConnection conn = null;
InputStreamReader in;
BufferedReader data;
String line;
StringBuffer buf = new StringBuffer();
try {
conn = this.page.openConnection();
conn.connect();
box.setText("Connection opened...");
in = new InputStreamReader(conn.getInputStream());
data = new BufferedReader(in);
box.setText("Reader data...");
while ((line = data.readLine()) != null) {
buf.append(line + "\n");
}
box.setText(buf.toString());
}
catch (IOException e) {
System.out.println("IO Error:" + e.getMessage());
}
}
}

Sincerely Quan Hodges

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
MackSix
Offline
Joined: 2013-03-27
Points: 0

Hi, you can't use the literal number one for a variable name. :)

Also, use the code tags (button with <>) when posting code so it is readable.