Question

How to call the main method?

void prompt()
{
    System.out.println("Do you want to continue?");
    Scanner confirm = new Scanner(System.in);
String con = confirm.nextLine();
if (con  == "y")
{
//call the main method once again.
}
}

When I use main(); It asks for the value of "args" yet I'm not sure what value I should put in it.

Was it helpful?

Solution

The main() method in a java program takes a String array argument.

public static void main(String[] args) {} 

If you do not use the variable args inside of main() you could just pass null to it. Otherwise you would need to pass a String array to the method.

However, you should not be calling the main() method from inside your application. The main() method should be used as an entry point into your application, to launch a program, not be used to recursively execute the logic inside that application. If you have functionality needed again you should put it in a separate method.

OTHER TIPS

Signature of main method is: public static void main(String[] args)

The main method accepts a single argument: an array of elements of type String.

public static void main(String[] args)

This array is the mechanism through which the runtime system passes information to your application. For example:

public static void main(String[] args) {
   System.out.println("args = " + args);
}

public static void prompt() {        
    System.out.println("Do you want to continue?");
    Scanner confirm = new Scanner(System.in);
    String con = confirm.nextLine();
    if (con  == "y") {

      String[] args = {<set string array>};
      main(args);

    }

}

For more details, look at this Oracle document: The main Method

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top