I tried using this code below

if (choice == 6)
                main (); //takes the user back to the main method to the Main Menu

but it won't compile.
error message:

No applicable overload for the method named "main" was found in type "movitetogo". Perhaps you wanted the overloaded version "void main(java.lang.String[]args)throws java.io.IOException;" instead?

I am looking for a simple way to just return back to the main method because I can't do all the programming in the main method, and loops drive me crazy, we are required to make new methods.

Thanks!

Recommended Answers

All 3 Replies

never try it..but i guess is className.main(null); or just main(null);

From a function, you just return to the calling function. If you need to return all the way back to main() you have two options:

1: In every function on the way from main() to this code, you must examine the return value and if it is a 'return-to-main' value then just return, else continue processing. And of course, at the level of your code snippet, you must return (something). Bear in mind that functions must return the same (almost) type everywhere they return at all. Although this option is feasible for small a program, it is not ideal for a variety of reasons.

2: Use an exception. In main(), call the top level function from inside a try block, and catch the 'Choice_was_6' exception in its catch block. In your code, throw an instance of 'Choice_was_6' exception class. The interpreter will unroll the call stack for you, and make sure that any 'finally' blocks are entered along the way, which is what you want.

3: (I always seem to find one more thing to say): Rethink your program: Why do you need to return to main() in this case? Is there a better way to think about program flow that will allow you to avoid the problem entirely?

i think option there in your previous message is the best for you, try and relax then think of how to reconstruct ur code snippet, there are 1001 means of achieving a same goal

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.