1

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.

Krunal
  • 77,632
  • 48
  • 245
  • 261
wormwood
  • 441
  • 4
  • 9
  • 17
  • 3
    Why you want to call the `main` method? Instead have the required functionality in a method and call it. It is really bad practice to have `main` doing lot of computations – Prateek Oct 02 '13 at 03:12
  • 3
    In a word -- don't! That smells of a very bad design. You're better off writing a logical program that makes sense and doesn't have this weird and bad smelling requirement. Why not use a while loop instead? Also, don't compare Strings with `==`. Use the `equals(...)` method instead. – Hovercraft Full Of Eels Oct 02 '13 at 03:12
  • `Class.main(args); Class.main(new String[]{});` This is how. Now follow Prateek advice.. – porfiriopartida Oct 02 '13 at 03:14
  • 4
    @porfiriopartida: now you will have to help him when he comes back with his recursive stackoverflow exception. – Hovercraft Full Of Eels Oct 02 '13 at 03:16
  • 1
    @HovercraftFullOfEels Don't worry, `con == "y"` will never be true.. oooh but you told him how to pass that condition. – porfiriopartida Oct 02 '13 at 03:20
  • 1
    Thanks for your answers. I will just transfer the codes to another method. – wormwood Oct 02 '13 at 03:42
  • @HovercraftFullOfEels Pleae check my answer :) – An SO User Oct 02 '13 at 04:50

2 Answers2

2

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.

Erich
  • 1,573
  • 1
  • 13
  • 21
0

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

Krunal
  • 77,632
  • 48
  • 245
  • 261