This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
Ok this one might be too hard to solve via forum, but I will throw it up here anyway.
I am writing an inventory management program and I am using if statements to decide what to do as the user enters a number in the command line.
The problem is... when it asks for a number and a number is entered it then asks for the number again, for what seems like a random number of times, and then it finally registers the number and does the right thing. To clear things up, I need it to ask for the number only once, and do the right thing the FIRST time it is entered.
This is the options class that determines which number is entered. and always reprints the menu, and asks for more input.
This is the menu class which has two methods, one to create the menu and another to grab the input, the second method could be the issue.
Lastly this is the class I use to run the whole thing, it first creates the menu and then decides on the input.
This has been a night long project and it is going well, minus this issue of reprinting the selection line over and over until it randomly is happy. Sorry for all of the code, but I think this is a good example for those who have a similar assignment. There are a couple of other classes like create inventory, but they do not have any impact on this issue.
Every time you type Menu.Select() you are making a fresh call to the Select() method in Menu. The Select() method prompts for input, and returns the result. What you want to do is call Manu.Select() only once and do something with the result:
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Joined: Jun 29, 2009
Ah ha, that makes sense, now I have some consistency, it only asks for the input twice.
Do you have any ideas as to why it needs the second input. Could it be because I am using Menu.Select(); to reprint the selection after the decision has been made and the actions taken.
Why do you put the code for creating the menu etc. at the end of every if statement?
If that needs to happen every time through the "loop until quit" loop then shouldn't it be written only once?
Re-organizing that might help answer your question.
Joined: Jun 29, 2009
After the actions are taken, the requirements are to reprint the menu, and ask for another selection.
The only way I can think of to do that is to place the menu create/select/option code at the end of each if statement. I cant think of a way that I could nest everything so that it knows when to print the menu with one line of create/select/option code, within the option class.
Another idea might be to rewrite the following code in a way that allows it to be placed at the end of each if statement.
I am thinking Option.userInput() is the culprit because it is not making a decision after the first input for some reason, but it does kick into gear on the second input.
Your original design is recursive: Option.userInput() calls itself. If you're not specifically trying to use recursion this will cause no end of grief.
In a program like this there's no reason to do this. The main loop of the program should be just that: a loop that accepts and processes user input until the user quits. I'm not sure which part of that you're having a problem with--maybe if you began down that path it would become clearer as you progressed?
Joined: Jun 29, 2009
Ok I have what I hope is the final question for this inventory project.
I am trying to access the .length or an array I have created to make this program more dynamic depending on how many elements are in the array.
This is my loop
This is the code that I am using to hard code my arrays for the time being
Would it be advisable that I somehow add a count to this, or is it possible to find out how many arrays are full with the above code?