🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

java hangman

Started by
6 comments, last by Toasted 22 years, 7 months ago
I am trying to make hangman in Java. I have a file set up from which I read from, I then store the individual words in a vector. I have tried to set up a loop that starts at 0 and goes to the word length, while in this loop I will check the letters and display them in the correct place. I can compile and run it, although it seems quite slow, and it also gives me an "Array out of bounds, 0 >= 0" remark. Unfortunately im quite new at this and things also seem to work......in theory. www.geocities.com/toasted_terran/hangman.java www.geocities.com/toasted_terran/hangman_graphics.java (Tell me if you want the source on the forum, if you can''t get to it) cheers
Advertisement
dude, i hope you aren''t trying to get someone to do your homework for you
OF course not, ive been trying to get this to work for about 3 months now.
hmmm sounds fishy to me ( homework) .... I will give you this for advice... read up on Exceptions and create a ''Try'' and ''Catch'' Block ...

      try       {         //What ever it is your doing       }    catch {ArrayIndexOutOfBounds yada yada e)       {         //Whatever you decide to do         System.out.println(e);       }    Finally       {          //Clean up       }  




Morpheus

----------

I disagree with a_insomniacs advice in this case. Tho you generally should try to handle exceptions, ArrayIndexOutOfBounds usually shouldn''t be caught this way.

This exception is telling you that there is a fundemental problem with the code and that it should be fixed, not just handled. Handling it will stop the error being shown but it is far better fixing the error.

Check to see if you''re accessing an element beyond the size of the array. Be aware that array indexes usually start at 0, and check your loops to make sure that this is how you are handling it.

However, it is good advice to read up on exceptions even tho it is not applicable in this case

--
SC
quote: Check to see if you''re accessing an element beyond the size of the array. Be aware that array indexes usually start at 0, and check your loops to make sure that this is how you are handling it.

quote: I then store the individual words in a vector.

Sorry, I missed the fact that you were using a vector and assumed it was an array, so some of that info was incorrect. However, the advice is still broadly applicable.
--
SC
I appreciate the (limited) help guys. But I think im on to it now. The vector was fine it was something else...
Although vectors start at 0. The charAt and/or elementAt may start their count at 1 (I don''t remember and don''t have the JDK right now).

This topic is closed to new replies.

Advertisement