*
The moose likes Beginning Java and the fly likes Weird multiple output issue with if statements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Weird multiple output issue with if statements" Watch "Weird multiple output issue with if statements" New topic
Author

Weird multiple output issue with if statements

Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
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.

Thanks for the help, this forum has been awesome.

Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
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
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
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.
Sumit Bisht
Ranch Hand

Joined: Jul 02, 2008
Posts: 329

" Could it be because I am using Menu.Select(); to reprint the selection after the decision has been made and the actions taken."
yes
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Ok so I changed the menu.Select in the if condition, but how would you change it after all the actions have been taken and the "Enter a selection" text needs to be reprinted?

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
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.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

My point was that since your goal is to ask for input forever until the user quits that this should be in a loop:
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
Sorry that dosent make sense to me. Could you provide the mehod, and possibly how I could add that to the option class.

I am a true greenhorn.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I won't provide an outright answer, no.

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?
Mike Osterhout
Ranch Hand

Joined: Jun 29, 2009
Posts: 84
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Weird multiple output issue with if statements