• Post Reply Bookmark Topic Watch Topic
  • New Topic

Menu Code repeating itself  RSS feed

 
Nayan Thaker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Beginning Java Forum members and leaders.

I'm very new to Java and am still influenced by my previous background with procedural programming.

I tried to write a nested menu with the console as the medium. Its quite likely that there might be reusable code available, but my attempt is really for practice to enable me to understand better how java code works.

The problem I'm encountering is that the Main Menu repeats itself. I'm guessing that it is because each time I make a method call, a new instance gets created.

To replicate the problem, the steps are:

1. Execute public class Rcase4
2. Main Menu displays
3. Select option 1 - Applications
4. Applications Menu displays
5. Select option 2 - Enter Application Codes
6. Test line prints, followed by Applications Menu. This is where code should stop and wait for user input, but instead, it continues and displays Main Menu as well

Any other tips to help me understand and write better Java code is also welcome

Thank you all in advance






 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You didn't have the code tags right; it tells you here how to use them. Don't worry; I have corrected them and doesn't it look better.
To repeat a piece of code you can simply wrap it into a loop. Since you probably want to run the code once and then consider repeat, a do loop might be appropriate.That means you read menuLevel from the Scanner; the () are needed to increase the precedence of the = operator, so the reading is done first. Then you test whether it equals "stop".

I can see you have done procedural programming; it takes a very long time for people to learn to use objects instead.
 
Nayan Thaker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Campbell,

Thank you for the welcome and pointing me in the right direction on the use of tags.

Thank you also for your response; I will make the changes you suggested and see how it goes.



  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!