简体   繁体   中英

Returning to the main method in java

How to return to the main method and execute again the whole program? I tried the return but it just ends the execution.

This is the code:

    File folder = new File("C:\my path");
    File[] listOfFiles = folder.listFiles();

        if (folder.isDirectory()){
            if(folder.list().length>0){
                System.out.println("Directory is not empty!");

            }
            else {
                System.out.println("Directory is empty!");
                return; //I need to return from the main method
            }
        }
        else{
            System.out.println("Invalid directory!");
        }

I want to return to the main method if the directory is empty. I used the return but, it did not return from the start :( Any idea on how to return to the main method? Thanks :)

This is the entire main method: (Sorry if it is too long) 在此处输入图片说明

在此处输入图片说明

在此处输入图片说明

My goal is to check if a new file entered the directory. Then it will list the files on that directory and one-by-one it will be deleted. If there's no file from the directory, it will return to the start.

Comment correction: "//It will return to the start"

The two loops: The second loop will be executed during the execution of the first loop. (Sorry if I did not indent it). After the execution of the second loop, it will return to the first loop. 在此处输入图片说明

It's hard to read but I assume you want to exit the loop using return statement which is not working that way. If you want to exit a loop you have to use break .

Here's a little bit of explanation: Difference between Return and Break statements

And since it's a basic programming knowledge I advice you to go through some Java tutorial first. For example: http://www.tutorialspoint.com/java/inde

The traditional way to return all the way to the top main method, is to throw a RuntimeException (or one of its subclasses) which is caught by the main method only. Any exception handlers on the way up, must rethrow this exception after doing what they need to.

Note that your application must be written very carefully to avoid leaking resources like opened files.

Don't do everything in main method. You must extract part of your code in other declared method like:

public static void doPartOfSomething() {
    ...
}

public static String doOtherPartOfSomething( String path ) {
    ...
    return "aresult"; // Stop the execution of this method 
}

Please note that the second one takes a parameter and returns a result when is called (a String here)

And then, from main method (or other place), you can call this piece of code several times:

doPartOfSomething();
doPartOfSomething();
String result = doOtherPartOfSomething( "/test" );
...

Doing that, you can execute some piece of code, return and stop a part of code, and continue with another call into the calling method.

I think you should learn more about classes, objects, methods and fields in java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM