• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with looping program  RSS feed

 
Nikki Harding
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating a word game for my java class. Most of it is working, but when I try to ask for a hint, it creates a new word before I can answer. I need help figuring out how to arrange my loop so that a new word is created when the program starts, but not when I ask for a hint. I understand why it's doing this, but I'm not sure how to fix it.



and here is the output of the program :
Enter in a file containing words (Ex: words.txt) :
words.txt
Current puzzle: farkn
Current points for word 10
Enter (g)uess, (n)ew word, (h)int, or (q)uit: h
The letter at spot 1 is f
Current puzzle: ikmsn
Current points for word 9
Enter (g)uess, (n)ew word, (h)int, or (q)uit: q
Goodbye!
Total score: 0


 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nikki,

You've gotten yourself into quite a sticky situation here.

First, you could really help yourself by reformatting your code so that it is properly indented and things line up nicely. When you don't line up your code, it's just a big jumble and it's hard to tell where one control structure (if-statements, while-loops, for-loops) ends and another begins or which one is nested inside which.

Second, have you learned about breaking down the problem into smaller chunks? This is done by putting each subtask into its own method. For example, from what I can tell, here's part of the narrative of what your program does:

The methods that are being called on the right hand side of the assignment statements would contain relevant sections of the code that you have all jammed together into your main method right now. This is called "decomposition", meaning that I've decomposed a large task with many detailed instructions into a number of smaller tasks, each with their own specific and more focused set of instructions.

Once you start organizing your program this way, you'll be better able to figure out how to fix the problem because the big picture will be easier to see. It won't be obscured by all the detailed instructions. In other words, you'll be able to see the forest from the trees if you decompose your program.

It's kind of like how your house has its own little rooms where you do specific things. Bedroom is for sleeping, kitchen is for cooking, garage is where you keep the car, bathroom is for private business and taking phone calls from your mom

Imagine you didn't have that and you only had one big, unorganized space in which to do everything. That would be a mess right? Now look at your main method.
 
Carey Brown
Saloon Keeper
Posts: 3309
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This block won't be executed unless generateNewWord is true. So far, so good. Once the block is done and the word is generated we no longer need to generate a new word (until we ask it to). So, we need to set this boolean to false somewhere in the block.

A general comment about the code, you've got too much crammed inside of main(). you should break some of it up into other new methods, e.g. loadWords(), scrambleWord(). I think with a little cleanup, things like current loop issue becomes easier to debug and modify.
 
Liutauras Vilda
Sheriff
Posts: 4914
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never write such things as:
That is very error prone, because by mistake you can easily manage to write:
and the block of code would get executed in both cases when generateNewWord is false or true.

Instead, write:

Class names suppose to start with an upper case, even if its name is "Test".
Junilu mentioned about decomposing your program, you can start using similar template to this:
When you decomposing your program into small methods, always spend enough time choosing accurate method name. Method name suppose to reveal its purpose. If you find it hard, most likely method is doing something wrong or just too many things.

Compiling. Don't write whole program in one go without compiling, running, testing. Compile often, every 5 or 10 lines you write. Why? The earlier you spot mistake, the easier going to be to fix that. For instance, if you know your lines 1 to 5 compiled just fine, and suddenly during your second compile after you wrote lines 6 to 10 your program does not compile - you KNOW, that something went wrong in the most recently written lines.
Testing. To test method you can simply call it by providing some test purpose inputs close to actual ones AND later some weird, to see if program works if you pass negative number, or if you pass the file path with "\" or "/" or any other unusual inputs you can think off. Remember, every fool can write a program which works with expected inputs, but YOU should try to write so it would work with every input. When you get full confident about method correctness - move towards the next one.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!