I've developed a code for a Hangman game and what I need help in is basically simplifying, correcting and removing parts of the code that are not needed (or there is an easier way to do it). I think there are parts where I could have done it easier but I do not know where they are. I would appreciate any/ all help and will welcome all constructive criticism.

2 Answers
2

Thank you for sharing your code. Quite frankly, it's a bit of a mess, so let's try to clean it up.

General Code Issues

Here are a few examples of issues with your code that can easily be corrected. All these issues can be automatically detected by a good IDE such as the free, open-source and excellent IntelliJ IDEA Community Edition1(Analyze->Inspect Code).

You import many classes that you never use. Remove all unnecessary imports, keeping only the ones your code requires. You should also be consistent in your use of * wildcards; either use them always or never.

Why are you suppressing a warning that is not affecting your code? Remove it.

@SuppressWarnings("serial")

Why are you declaring but never using pnl3, pnl4, pnl5 and pnl6? Remove them.

Why are you declaring the constructor and main as throws Exception when they don't? Remove. The same goes for throws IOException on createPuz.

Package names in Java are expected to be lower-case,
so you should change package Hangman; to package hangman;

Version logs should be kept in source control, not in the source files (I noticed this in your upload)

Remove redundant comments that say nothing, such as this one:

// Panels where everything is drawn on

There's no need to add compare Boolean values with true or false literals, just use them directly:

this is amazing, just out curiosity do you have like a checklist of things you check throughout the code and if you do, perhaps you could tell me because instead of bringing a fully raw code, i could self check first?
–
ExikleFeb 11 '13 at 3:27

so i have done all this, as well i also have modified the code using the program you mentioned (pretty cool program) and have created a custom JButton class and shortened the code. I have uploaded the code on Git and here too.
–
ExikleFeb 12 '13 at 8:19

I'd like to focus on the getPuz() function to make the work of codesparkle more manageable. :)

Exceptions handling

The first, high-level problem is the way you use try/catch blocks. Exceptions are not here to get into your way! They're great tools that let you write robust programs and focus on error conditions only where necessary. Writing "Problem opening file" and continuing execution as if nothing had happened is a really bad idea. The truth is that if you can't open that file then your program is useless. So you might as well show the error to your users and quit the program once they acknowledged it. I removed all try/catch blocks from the code since your function already says throws Exception and because getPuz() is not the place to handle exceptions.

Declarations

Why "A B 1"? When programming, you optimize for reading: reading your code should be effortles. This is not the case when an arbitrary string shows up like this. Also try avoiding setting anything to null when possible. Here this means not declaring the BufferedReader right away.

You're going through a lot of trouble to count the number of lines in the file. The first think a reader thinks when looking at this is "wtf?". This is where good comments can help: you could have written "Count the number of lines in f". But the better thing to do is to actually use a better container for allPuz. You could use a ArrayList instead which allows you to append new elements without losing performance. This would allow you to change the real reading code:

Thanks - and good review! But I'd suggest ArrayList<E> instead of Vector<E> as the latter is considered outdated (it synchronizes each individual operation, which doesn't have any benefits for concurrency but slows down the code. Also, the name Vector is misleading).
–
codesparkleFeb 11 '13 at 10:12