Find 10 bad things about this piece of Java Code! :D :D

I am having this assignment in which I need to find 10 bad things about this piece of java code. I have never used Java before,, so please help.

ps. anything goes, such as bad comments, ways to make the code more concise, bad format, or whatever! Thanks a lot in advance

//**~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~

// File: cnnCrawler.java

//

// This code looks at the CNN website and follows some links to get info on articles that I want more

// info on.

// All output is written in the working directory to: cnnCrawlerOutput.txt

//**~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~

import gnu.regexp.*;

import java.net.*;

import java.io.*;

public class cnnCrawler{

public static void main(String[] args)

{

StringBuffer basePage = new StringBuffer();

// Connect to CNN and get the document

basePage = getBasePageContents("http://www.cnn.com");

// Look at the area of interest (The "MORE FROM CNN" section)

basePage = initialIsolateBasePageContents(basePage);

// Pull all of the URLs out

basePage = getInfo(basePage, "?lt;a href=\"[^\"]*|/b> <a href=\"[^>]*|/b><a href=\"[^>]*");

basePage = getInfo(basePage, "\"/[^(\")]*");

basePage = getInfo(basePage,"\"[^&]*");

// Go to the URLs and pull out the information of interest and

// write to file.

goToURLs(basePage);

}

//**~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~

// Method: getBasePageContents

//

// This method opens a connection to the webpage we are interested in and stores

// all of the text on the page

//**~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~

public static StringBuffer getBasePageContents(String myURL){

try{

// Set base document to CNN, open connection,

// and copy the source text into a buffer

URL cnnBaseDoc = new URL(myURL);

cnnBaseDoc.openConnection();

BufferedReader cnnBaseBuffer = new BufferedReader(

new InputStreamReader(

cnnBaseDoc.openStream()));

String cnnBaseInputLine;

StringBuffer tempDocument = new StringBuffer();

while ((cnnBaseInputLine = cnnBaseBuffer.readLine()) != null){

tempDocument.append(cnnBaseInputLine);

}

cnnBaseBuffer.close();

return(tempDocument);

}

catch(MalformedURLException e) {

System.out.println("Unable to create URL object");

return(null);

}

catch(IOException e){

System.out.println("Unable to open URL");

return(null);

}

}

//**~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~

// Method: initialIsolateBasePageContents

//

// This method isolates us to store only the section we are interest in --

// the "MORE FROM CNN" section

//

//**~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~

public static StringBuffer initialIsolateBasePageContents(StringBuffer basePage){

try{

RE document = new RE(basePage);

// Define the left and right isolators

String sLeft = new String("MORE FROM CNN[//w//W]*");

RE leftCntxt = new RE(sLeft);

RE rightCntxt= new RE("><b>SPORTS");

StringBuffer sLIsolator = new StringBuffer("");

int iLIsolatorIndex = 0;

RE regLIsolator = new RE(leftCntxt);

REMatch ctxtLMatch = regLIsolator.getMatch(basePage);

sLIsolator.append(ctxtLMatch.toString());

iLIsolatorIndex = ctxtLMatch.getStartIndex();

// Find the Right Isolator

StringBuffer sRIsolator = new StringBuffer();

RE regRIsolator = new RE(rightCntxt);

int iRIsolatorIndex = 0;

REMatch ctxtRMatch = regRIsolator.getMatch(basePage);

sRIsolator.append(ctxtRMatch.toString());

iRIsolatorIndex = ctxtRMatch.getStartIndex();

basePage.delete(iRIsolatorIndex, basePage.length());

basePage.delete(0, iLIsolatorIndex);

return(basePage);

}

catch(REException e){

System.out.println("RE Exception");

return(null);

}

}

//**~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~

// Method: getInfo

//

// This method applies the specified regular expression to the string passed in

//**~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~

public static StringBuffer getInfo(StringBuffer textToSearch, String regExp){

try{

StringBuffer sIsolated = new StringBuffer("");

int iLIsolatorIndex = 0;

String sLeft = new String(regExp);

RE leftCntxt = new RE(sLeft);

RE regLIsolator = new RE(leftCntxt);

REMatchEnumeration ctxtLMatch = regLIsolator.getMatchEnumeration(textToSearch);

while (ctxtLMatch.hasMoreMatches()){

sIsolated.append(ctxtLMatch.nextMatch().toString());

sIsolated.append("\n");

}

return(sIsolated);

}

catch(REException e){

System.out.println("RE Exception");

return(null);

}

}

public static void goToURLs(StringBuffer textToSearch)

{

try{

StringBuffer interestingDoc = new StringBuffer("");

StringBuffer sInfoForFile = new StringBuffer("");

int numPage=0;

FileOutputStream fCnnOut;

PrintStream pCnnOut;

String sLeft = new String("/[^\"]*");

RE leftCntxt = new RE(sLeft);

String sIsolated = new String();

int iLIsolatorIndex = 0;

RE regLIsolator = new RE(leftCntxt);

REMatchEnumeration ctxtLMatch = regLIsolator.getMatchEnumeration(textToSearch);

fCnnOut = new FileOutputStream("cnnCrawlerOutput.txt");

pCnnOut = new PrintStream(fCnnOut);

while (ctxtLMatch.hasMoreMatches())

{

numPage++;

sIsolated = "http://www.cnn.com";

sIsolated += (ctxtLMatch.nextMatch().toString());

interestingDoc = connectToURLs(sIsolated);

sInfoForFile = getDocInfo(interestingDoc, sIsolated, numPage);

pCnnOut.println (sInfoForFile);

}

pCnnOut.close();

System.out.println("You may view the output in file: cnnCrawlerOutput.txt.");

}

catch(REException e){

System.out.println("RE Exception");

}

catch (Exception e)

{

System.out.println ("Error writing file.");

}

}

//**~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~

// Method: connectToURLs

// This method opens a URL and returns the text of the page

//**~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~*~

public static StringBuffer connectToURLs(String urlText){

try{

URL cnnBaseDoc = new URL(urlText);

cnnBaseDoc.openConnection();

BufferedReader cnnBaseBuffer = new BufferedReader(

new InputStreamReader(

cnnBaseDoc.openStream()));

String cnnBaseInputLine;

StringBuffer tempDocument = new StringBuffer();

while ((cnnBaseInputLine = cnnBaseBuffer.readLine()) != null){

tempDocument.append(cnnBaseInputLine);

}

cnnBaseBuffer.close();

return(tempDocument);

}

catch(MalformedURLException e) {

System.out.println("Unable to create URL object");

return(null);

}

catch(IOException e){

System.out.println("Unable to open URL");

return(null);

}

}

Message was edited by:

rockingsoul

[7678 byte] By [rockingsoula] at [2007-11-26 18:31:06]
# 1

I could name a few. But it's your homework to do that, isn't it? Posting your homework here and inviting us to do it isn't really ethical. (Others would put it more strongly.)

So how about if you post some of your 10 top bad things about the code, with explanations, and we can discuss them?

DrClapa at 2007-7-9 6:05:14 > top of Java-index,Java Essentials,Java Programming...
# 2
Any ideas? Anything goes! Like, "I like nachos! And this code does not mention nachos so I hate it" I am desperate. I am on my knees. :(
rockingsoula at 2007-7-9 6:05:14 > top of Java-index,Java Essentials,Java Programming...
# 3
The formatting is terrible. Who left aligns all the code?
camickra at 2007-7-9 6:05:14 > top of Java-index,Java Essentials,Java Programming...
# 4
You said "I have never used Java before." Then who gave you this assignment and why? Or did you just skip the first ten weeks of the course?
DrClapa at 2007-7-9 6:05:14 > top of Java-index,Java Essentials,Java Programming...
# 5

one thing I can think of is to make all the methods private. And, maybe some of the catch blocks do not have return null? Maybe some of the statements are redudant, such as the programmer used "basePage" three times in the main, maybe it can be combined to one?

I am not trying to cheat here, just looking for some ideas so I can get this thing started, so I can look for similar bad patterns in the code. I have never programmed in Java before.

rockingsoula at 2007-7-9 6:05:14 > top of Java-index,Java Essentials,Java Programming...
# 6
I do not know why, I guess the teacher assumed we know Java? She came from a university that teaches Java to students. but we teach C++ here at our place.Message was edited by: rockingsoul
rockingsoula at 2007-7-9 6:05:14 > top of Java-index,Java Essentials,Java Programming...
# 7

> Any ideas? Anything goes! Like, "I like nachos! And

> this code does not mention nachos so I hate it" I am

> desperate. I am on my knees. :(

Just because Ur on ur Knees ;-)

basePage = getBasePageContents("http://www.cnn.com");

Check out what the method returns when ter s some exception

-TC

AbiSSa at 2007-7-9 6:05:14 > top of Java-index,Java Essentials,Java Programming...
# 8
it returns Null? I guess that is bad since the code in main does not do anything to handle the Null situation, correct?I love u, Give me some more and I will be your cyber peach.
rockingsoula at 2007-7-9 6:05:14 > top of Java-index,Java Essentials,Java Programming...
# 9
> I love u, Give me some more and I will be your cyber> peach.What is a cyber peach ?
AbiSSa at 2007-7-9 6:05:14 > top of Java-index,Java Essentials,Java Programming...
# 10
It is like, something submissive and peachy, I suppose :DAnyway, is there anything I can do to make the code more concise? Combining some statements?
rockingsoula at 2007-7-9 6:05:14 > top of Java-index,Java Essentials,Java Programming...
# 11
Have a look and see if all the variable initializations are necessary.
ejpa at 2007-7-9 6:05:14 > top of Java-index,Java Essentials,Java Programming...
# 12

> Have a look and see if all the variable

> initializations are necessary.

Thanks a lot for the hint, I found one but I am not sure if I am right:

For example, the method 搃nitialIsolatedBasePageContents?has a statement:

int iLIsolatorIndex = 0;

But a few lines later another statement:

iLIsolatorIndex = ctxtLMatch.getStartIndex();

Set the value of iLIsolatorIndex according to this function. So it really is not necessary to initialize iLIsolatorIndex to 0 in the first place.

I am also looking at Class initializations, anything wrong with that?

rockingsoula at 2007-7-9 6:05:14 > top of Java-index,Java Essentials,Java Programming...
# 13
What i don't like is that the class name starts with small letter.Genereally class name starts with capital letter.
lupansanseia at 2007-7-9 6:05:14 > top of Java-index,Java Essentials,Java Programming...
# 14
> What i don't like is that the class name starts with> small letter.> > Genereally class name starts with capital letter.Thanks, that is great. I really did not know that. If I could, i would give u a hug!
rockingsoula at 2007-7-9 6:05:14 > top of Java-index,Java Essentials,Java Programming...
# 15
Thanks to lupan, ejp, AbiSS who have landed their hands to help me!I am like super grateful, ya.I just need 4 more bad ponits about this code... I think I am doing better than most of my classmates for now. anything else?
rockingsoula at 2007-7-21 17:23:10 > top of Java-index,Java Essentials,Java Programming...
# 16

String sLeft = new String(regExp);

RE leftCntxt = new RE(sLeft);

RE regLIsolator = new RE(leftCntxt);

I dont think so many lines of code are required to do the operation..

I'm not sure though ;-)

AbiSSa at 2007-7-21 17:23:10 > top of Java-index,Java Essentials,Java Programming...
# 17

> String sLeft = new String(regExp);

> RE leftCntxt = new RE(sLeft);

> RE regLIsolator = new RE(leftCntxt);

>

> I dont think so many lines of code are required to do

> the operation..

>

> I'm not sure though ;-)

I would def. blow u if I was drunk!

rockingsoula at 2007-7-21 17:23:10 > top of Java-index,Java Essentials,Java Programming...
# 18
> Thanks a lot for the hint, I found one but I am not> sure if I am right:You are right, and there are many more than one.
ejpa at 2007-7-21 17:23:10 > top of Java-index,Java Essentials,Java Programming...
# 19

> > I dont think so many lines of code are required to

> do

> > the operation..

> >

> > I'm not sure though ;-)

>

> I would def. blow u if I was drunk!

RE regLIsolator = new RE(new RE(new String(regExp)));

This is how man ;-)

I'm goin off now... bye... Blow me sme other day

-TC

AbiSSa at 2007-7-21 17:23:10 > top of Java-index,Java Essentials,Java Programming...
# 20
Thanks. ejpMessage was edited by: rockingsoul
rockingsoula at 2007-7-21 17:23:10 > top of Java-index,Java Essentials,Java Programming...
# 21
That is a sweet one! TCMessage was edited by: rockingsoul
rockingsoula at 2007-7-21 17:23:10 > top of Java-index,Java Essentials,Java Programming...
# 22
Indentation,JavaDoc,(Seem to be the most obvious)Regards,Sim085
sim085a at 2007-7-21 17:23:10 > top of Java-index,Java Essentials,Java Programming...