0

Rerun program

If the user selects 1 ، she will exit the program If selects 2 , the program will rerun again What do i do main method run again?

16th Apr 2023, 10:21 AM
3axpa
3axpa - avatar
15 Antworten
+ 8
You can use a while-loop. Get input in the loop, check if the input is 1 or 2, proceed correspondingly. If you are trying to do this on sololearn Java playground, note at playground isn't interactive.
16th Apr 2023, 10:31 AM
Lisa
Lisa - avatar
+ 5
a while loop, or an if else loop would probably work
16th Apr 2023, 10:31 AM
Bob_Li
Bob_Li - avatar
+ 5
if (select == 2) main(args); else if (select==1) System.exit(0); else;//Whatever you want
16th Apr 2023, 10:39 AM
Binx
Binx - avatar
+ 4
Attempt?
16th Apr 2023, 10:24 AM
Sakshi [Offline 🙃]
Sakshi [Offline 🙃] - avatar
+ 3
Snehil thank you :)
16th Apr 2023, 10:43 AM
3axpa
3axpa - avatar
+ 2
Snehil The problem with the sample you provided is that every time the user selects 2, it creates another level of recursion but never returns back until the user exits the program, not releasing the resources it might hold. And memoization will not help here. The proper way is already described in the Lisa's answer: create a loop, read the user input in that loop, and action according to that. Also main() isn't supposed to be called. Doing that you confuse the future readers of your code and possibly yourself.
18th Apr 2023, 9:56 AM
Евгений
Евгений - avatar
+ 2
3axpa You started the thread yet seems to totally ignore it... Anyway, here is a sample do while loop. You constantly check the sc.nextLine() until the user inputs the exit code (in this case, "x") which breaks the loop and exit the program. Instead of println, you can execute your code instead. I used the hasNextLine() instead of true to get rid of the error message you get if you break a loop that is expecting a user input. Of course in Sololearn you cannot really experience the interactive loop, since all the inputs have to be provided in the submit popup. But if you compile and run the code in a proper terminal, it should give you a continuous input and printout loop without error messages when you input the exit string ("x") to exit the program. https://code.sololearn.com/c435ehTUIZ7Y/?ref=app
18th Apr 2023, 12:59 PM
Bob_Li
Bob_Li - avatar
+ 1
Snehil Pls avoid giving finished code as answer, as this makes the OP skip the most important part of learning. Prefer giving hints for the OP to find the solution instead.
16th Apr 2023, 3:41 PM
Emerson Prado
Emerson Prado - avatar
+ 1
Snehil This is a bad advice. The solution you proposed imposes a memory leak. 3axpa Note that.
18th Apr 2023, 9:37 AM
Евгений
Евгений - avatar
+ 1
Snehil Yes that's the common cause of memory leaks. But it's not the only one. Another one is improper use of recursive functions as in your example.
18th Apr 2023, 9:42 AM
Евгений
Евгений - avatar
+ 1
Snehil Your question cannot be answered in general. How do you avoid crossing the street on the red signal? Well, you just avoid. You pay attention. For example, don't read your phone near the crossing. Or, if you do, then stand still and don't move while you read. What are the other possibilities and how to avoid them? There are a lot, and it's hard to count them all, not to mention solving them. Same here. First and simple don't use potentially dangerous things if they're not needed. If you need to, then understand how they work, all the dangers and consequences. Don't use resursion without a real need. Follow best practices and common sense. Don't call the main() function as it's not supposed to be called inside the program itself.
18th Apr 2023, 10:27 AM
Евгений
Евгений - avatar
0
Emerson Prado that's a basic logic not the code
16th Apr 2023, 3:43 PM
Binx
Binx - avatar
0
Евгений memory leaks often occur when dynamic memory allocation is used and not properly released
18th Apr 2023, 9:39 AM
Binx
Binx - avatar
0
Евгений ok so there are chances when the load is too high for recursion, in this case we can either have looping or Stack data structure but is there any better option other than this? memoization technique can't be preferred right here
18th Apr 2023, 9:47 AM
Binx
Binx - avatar
0
Евгений sorry that's true that there's no scope of memoization. but as the user asked to rerun main() so I just provided a naive code. I think if we're working on true projects then for such particular condition I would prefer Stack Data Structure. if you don't mind can you please give me some more ways to avoid memory leak
18th Apr 2023, 10:00 AM
Binx
Binx - avatar