help with code!

brokenjago

Newbie
Joined
Jul 9, 2003
Messages
328
Reaction score
0
Help/Advice with code! Thanks!

Hi guys, I'm new on this particular forum. I usually hang out around the HL2 Discussion, and the Off Topic Forums, but I thought I'd try my hand here.

Anyway, I've been coding in java for about 8 months now, I was in a program in my school called the Oracle Database Interent Academy, a two year program in which you familiarise yourself with databases, and the programming behind them, etc etc...

I decided that I enjoyed coding immensely, and have since continued past my graduation (I graduated in June). I was pondering what to make when a friend came up to me with a plea:

"Jago <--(that's my name), I want to switch from Trillian to GAIM, but I need my Trillian Logs with GAIM. "Hhhhhmmmm," I thought, "This presents an interesting program challenge that would actually be useful to someone! Let the coding begin!"

I soon realized that I needed to take baby steps toward my ultimate goal: I quickly made a basic program that writes a crappy ol' string to a file. That was a success. The next phase, I decided, was to create a program that accepts user input, then generates a web page (fully XHTML 1.0 Strict and CSS valid ;)), with a picture and basic info about that person. That's what I'm doing now. So far, it's come along swimmingly, and I'm doing well. I don't really have any problems with the code, other than minor annoyances, which I will address later. I was just wondering what you think of the code for now. Whether it's well made, whether I used good/efficient ways, etc, etc.

Well, without further ado, here is my code:

Code:
/*********************************************************************
 * Automatic HTML profile Creator by Jago Goddard. This program is   *
 * released in accordance with the GNU General Public Lisence. You   *
 * are free to distrubute the program, and/or use portions of it     *
 * in your own projects so long as you give the original author (me) *
 * credit. Have fun, and remember kids, don't do drugs!              *
 *********************************************************************/

//all HTML used by this program is actually completely XHTML 1.0 Strict and CSS3 compliant, Verify it with the W3, if you must...
//before anything happens, please realize that I have very bad spelling, So please excuse any errors I might make in my comments

import java.io.*;

class gimmeHTML{

	FileWriter HTMLDocWriter, CSSWriter;
	String firstName, lastName;

	gimmeHTML(String fName, String lName)
	{
		firstName = fName;
		lastName = lName;

		try
		{

			HTMLDocWriter = new FileWriter(firstName + "'s Profile.html");
			CSSWriter = new FileWriter("Style Sheet.css");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: FileWriter creation failed. Check folder and/or file permissions and try again.");

		}
	}

	void brIt()
	{
		try
		{

			HTMLDocWriter.write("[br]");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}
	}

	void nbspIt(int quantity)
	{
		try
		{

			for(int x=0;x<=quantity;x++)
			{
				HTMLDocWriter.write("");
			}

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}
	}

	void constructCSSDoc()
	{
		try
		{

			CSSWriter.write("p.right {text-align: right} p.left {text-align: left} p.center {text-align: center} img {border-width: 0} a {border-width: 0}");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Write to CSS file failed. File will have no formatting.");

		}
	}


	void writeIt(String string)
	{
		try
		{

			HTMLDocWriter.write(string);

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}

	}

	void horizontalRule()
	{
		try
		{

			HTMLDocWriter.write("<hr />");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}

	}

	void constructDocumentOpen()
	{
		try
		{

			HTMLDocWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?> <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> <html> <head><link rel=\"stylesheet\" type=\"text/css\" href=\"Style Sheet.css\" /> <title>Profile Creator 1.0 - "+firstName+"'s Profile</title></head><body>");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}
	}

	void writeCSSValid()
	{
		try
		{
			HTMLDocWriter.write("[url=\"http://jigsaw.w3.org/css-validator/\"][img]\"http://jigsaw.w3.org/css-validator/images/vcss\" alt=\"Valid CSS!\" height=\"31\" width=\"88\" />[/url]");
		}
		catch(IOException exc)
		{
			System.out.println("Fatal Error: Could not write to CSS document. Please check file permissions and try again.");
		}
	}

	void writeXHTMLValid()
	{
		try
		{

			HTMLDocWriter.write("[url=\"http://validator.w3.org/check?uri=referer\"][img]\"http://www.w3.org/Icons/valid-xhtml10\" alt=\"Valid XHTML 1.0!\" height=\"31\" width=\"88\" />[/url]");
		}
		catch(IOException exc)
		{
			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");
		}
	}


	void constructDocumentClose()
	{
		try
		{
			HTMLDocWriter.write("</body></html>");
		}
		catch(IOException exc)
		{
			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");
		}
	}

	void alignItOpen(String alignment)
	{

		if(alignment != "center" && alignment != "right" && alignment != "left")
		{

			System.out.println("Invalid alignment chosen. Defaulting to \"center\"");

			alignment = "center";

		}

		try
		{

			HTMLDocWriter.write("<p class=\""+alignment+"\">");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}

	}

	void alignItClose()
	{
		try
		{

			HTMLDocWriter.write("</p>");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}
	}


	void boldItOpen()
	{
		try
		{

			HTMLDocWriter.write("[b]");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}

	}

	void boldItClose()
	{
		try
		{

			HTMLDocWriter.write("[/b]");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}
	}

	void italicizeItOpen()
	{
		try
		{

			HTMLDocWriter.write("[i]");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}
	}

	void italicizeItClose()
	{
		try
		{

			HTMLDocWriter.write("[/i]");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}
	}

	void underlineItOpen()
	{
		try
		{

			HTMLDocWriter.write("<u>");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}
	}

	void underlineItClose()
	{
		try
		{

			HTMLDocWriter.write("</u>");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}
	}

	void linkItOpen(String link)
	{
		try
		{

			HTMLDocWriter.write("[url=\""+link+"\"]");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}
	}

		void linkItClose()
		{
		try
		{

			HTMLDocWriter.write("[/url]");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}
	}

	void imageIt(String src){
		try
		{

			HTMLDocWriter.write("[img]\""+src+"\" id=\""+firstName+"'s Picture\" />");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");

		}
	}
}

class profileCreator{
	public static void main(String args[]){

		try
		{

			gimmeHTML blah = new gimmeHTML("test","man");
			blah.constructCSSDoc();
			blah.CSSWriter.close();
			blah.constructDocumentOpen();
			blah.alignItOpen("center");
			blah.writeXHTMLValid();
			blah.nbspIt(2);
			blah.writeCSSValid();
			blah.alignItClose();
			blah.constructDocumentClose();
			blah.HTMLDocWriter.close();

		}
		catch(IOException exc)
		{
			System.out.println("Fatal Error: File Creation failed.");
		}
	}
}

Sorry for a lack of comments, I previously had everything carefully commented, but then I re-wrote half the program and all the comments became obsolete. It is now my policy to write comments only after the code is finalized.
 
Also, as you'll observe on line 130:
Code:
HTMLDocWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?> <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> <html> <head><link rel=\"stylesheet\" type=\"text/css\" href=\"Style Sheet.css\" /> <title>Profile Creator 1.0 - "+firstName+"'s Profile</title></head><body>");

I was wondering if there is a way to write line breaks to a file? I tried "\n", but it didn't work. this is only a minor annoyance, but when I open up the newly created HTML file in notepad to inspect it, it gets rather hard to read when it's all on one line. Thanks for any help!

Edit: I'd like to point out that this isn't an argument of which IM program is better, so please don't make it one. And please don't tell me to do it in C++, that's not what I'm here for.
 
Oh, I forgot to mention: the actual executed commands (class ProfileCreator), those are just test commands, they write the little "XHTML Valid!" and "CSS Valid!" Icons to a page. With two 's between them.

Edit: Woo! Triple post! :|
 
You use the code
Code:
try
{
	HTMLDocWriter.write(X);
}
catch(IOException exc)
{
	System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");
}

Where X is different each time. Why not create a private function to do this for you then just call it, then you could replace that entire chunk of code with a single statement.

So...

Code:
/******************************  ******************************  *********
 * Automatic HTML profile Creator by Jago Goddard. This program is   *
 * released in accordance with the GNU General Public Lisence. You   *
 * are free to distrubute the program, and/or use portions of it     *
 * in your own projects so long as you give the original author (me) *
 * credit. Have fun, and remember kids, don't do drugs!              *
 ******************************  ******************************  *********/

//all HTML used by this program is actually completely XHTML 1.0 Strict and CSS3 compliant, Verify it with the W3, if you must...
//before anything happens, please realize that I have very bad spelling, So please excuse any errors I might make in my comments

import java.io.*;

class gimmeHTML{

	FileWriter HTMLDocWriter, CSSWriter;
	String firstName, lastName;

	gimmeHTML(String fName, String lName)
	{
		firstName = fName;
		lastName = lName;

		try
		{

			HTMLDocWriter = new FileWriter(firstName + "'s Profile.html");
			CSSWriter = new FileWriter("Style Sheet.css");

		}
		catch(IOException exc)
		{

			System.out.println("Fatal Error: FileWriter creation failed. Check folder and/or file permissions and try again.");

		}
	}

	void WriteHMTL(String source)
	{
		try
		{
			HTMLDocWriter.write(source);
		}
		catch(IOException exc)
		{
			System.out.println("Fatal Error: Could not write to HTML document. Please check file permissions and try again.");
		}
	}

	void WriteCSS(String source)
	{
		try
		{
			CSSWriter.write(source);
		}
		catch(IOException exc)
		{
			System.out.println("Fatal Error: Write to CSS file failed. File will have no formatting.");
		}
	}

	void brIt()
	{
		WriteHMTL("[br]");
	}

	void nbspIt(int quantity)
	{
		for(int x=0;x<=quantity;x++)
		{
			WriteHMTL("");
		}
	}

	void constructCSSDoc()
	{
		WriteCSS("p.right {text-align: right} p.left {text-align: left} p.center {text-align: center} img {border-width: 0} a {border-width: 0}");
	}


	void writeIt(String string)
	{
		WriteHMTL(string);
	}

	void horizontalRule()
	{
		WriteHMTL("<hr />");
	}

	void constructDocumentOpen()
	{
		WriteHMTL("<?xml version=\"1.0\" encoding=\"UTF-8\"?> <!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"> <html> <head><link rel=\"stylesheet\" type=\"text/css\" href=\"Style Sheet.css\" /> <title>Profile Creator 1.0 - "+firstName+"'s Profile</title></head><body>");
	}

	void writeCSSValid()
	{
		WriteHMTL("[url=\"http://jigsaw.w3.org/css-validator/\"][img]\"http://jigsaw.w3.org/css-validator/images/vcss\" alt=\"Valid CSS!\" height=\"31\" width=\"88\" />[/url]");
	}

	void writeXHTMLValid()
	{
		WriteHMTL("[url=\"http://validator.w3.org/check?uri=referer\"][img]\"http://www.w3.org/Icons/valid-xhtml10\" alt=\"Valid XHTML 1.0!\" height=\"31\" width=\"88\" />[/url]");
	}


	void constructDocumentClose()
	{
		WriteHMTL("</body></html>");
	}

	void alignItOpen(String alignment)
	{
		if(alignment != "center" && alignment != "right" && alignment != "left")
		{

			System.out.println("Invalid alignment chosen. Defaulting to \"center\"");

			alignment = "center";

		}

		WriteHMTL("<p class=\""+alignment+"\">");
	}

	void alignItClose()
	{
		WriteHMTL("</p>");
	}


	void boldItOpen()
	{
		WriteHMTL("[b]");
	}

	void boldItClose()
	{
		WriteHMTL("[/b]");
	}

	void italicizeItOpen()
	{
		WriteHMTL("[i]");
	}

	void italicizeItClose()
	{
		WriteHMTL("[/i]");
	}

	void underlineItOpen()
	{
		WriteHMTL("<u>");
	}

	void underlineItClose()
	{
		WriteHMTL("</u>");
	}

	void linkIt(String link, String text)
	{
		WriteHMTL("[url=\""+link+"\"]"+text+"[/url]");
	}

	void imageIt(String src)
	{
		WriteHMTL("[img]\""+src+"\" id=\""+firstName+"'s Picture\" />");
	}
}

class profileCreator{
	public static void main(String args[])
	{
		
			gimmeHTML blah = new gimmeHTML("test","man");
			blah.constructCSSDoc();
			blah.CSSWriter.close();
			blah.constructDocumentOpen();
			blah.alignItOpen("center");
			blah.writeXHTMLValid();
			blah.nbspIt(2);
			blah.writeCSSValid();
			blah.alignItClose();
			blah.constructDocumentClose();
			blah.HTMLDocWriter.close();
	}
}

This just seems a tad easier to read...
 
You might as well get more familiar with package java.io. Read up on BufferedWriter and PrintWriter in the API. Inserting the newline character(s) manually is a bad idea because it likely won't work under a different OS. Using BufferedWriter's newLine or PrintWriter's println is a much better approach, and println won't even throw an exception.
 
Yeah, I was trying to do that, but the Java API documentation is so annoying to search, it always brings up these bogus results that end up getting me confused....

Akrin, I was just thinking of a way to make it so i didn't have to use so many try/catch statments. Thanks for the tip, I will be implementing it.
 
Back
Top