How to call main from inside class

10,118

Solution 1

I noticed you were trying to repeatedly do what is in the main method.

Rather than calling the main method, which is something that is regarded as a bad design decision, you can call something repetitively.

If you want to do something repetitively, you can use this form:

public static void main(String[] args) {
    boolean endCondition = false;

    while(!endCondition) {
        Chemicalcommndline.start();
        endCondition = shouldEndCheck();
    }
}

where the shouldEndCheck method returns true if you should stop doing the looping.

If you want to check for valid input, you can use this form:

public static void main(String[] args) {

    String input = "";
    do {
        input = readInput();
    } while (!validInput(input));

    processInput(input);
}

readInput returns a String provided by the user (it could be something simpler), validInput is a boolean method and returns true if the input is considered valid by you, and processInput is what you choose to do with a valid input.

I hope this helps.

Solution 2

No you cant invoke main this way. Use a while loop with some form of terminal condition in the input reading.

Solution 3

Why do you want to call main? from your start method? !! this will end in an infinite recursive calls. Main will call start and start will call main Though if you insist in doing it you can do the following. See how the start method signature changes and how we pass null to main. In your case this program will never end unless you write special handling code to do "system.exit" on entering "q" or something.

import java.io.*;
public class Chemicalcommandline {
public static void start()  {
    String instructions = "This program "
            + ""
            + ""
            + ""
            + "";
    System.out.println();
     System.out.println("Chemical Sign: ");

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String chemical = null;
    try {
        chemical = reader.readLine();
    }   catch(IOException ioe)  {
        System.out.println("Error");
    }
   //start crazy long if
if (chemical.equals("Ca")) {
    System.out.println("Calcium");

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

  Chemicalcommandline.start();
}
}  

Suggested

import java.io.*;
public class Chemicalcommandline {
public static void start()  {
    String instructions = "This program "
            + ""
            + ""
            + ""
            + "";
    System.out.println(instructions);
     System.out.println("Chemical Sign: ");

    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String chemical = null;
    try {
        chemical = reader.readLine();
        if(chemical.equals("exit")){
            System.exit(0);
        }
    }   catch(IOException ioe)  {
        System.out.println("Error");
    }
   //start crazy long if
if (chemical.equals("Ca")) {
    System.out.println("Calcium");

}
main(null);
}
public static void main(String[] args)  {
  //Chemicalcommandline client = new Chemicalcommandline();
  Chemicalcommandline.start();
}
}  
Share:
10,118
Icallitvera
Author by

Icallitvera

Updated on June 13, 2022

Comments

  • Icallitvera
    Icallitvera about 2 years

    I and really new to Java and I'm sure that there is a way to do this so I'm going to ask: Can you call the main method from the class?

     import java.io.*;
    public class Chemicalcommandline {
    public void start()  {
        String instructions = "This program "
                + ""
                + ""
                + ""
                + "";
        System.out.println();
         System.out.println("Chemical Sign: ");
    
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String chemical = null;
        try {
            chemical = reader.readLine();
        }   catch(IOException ioe)  {
            System.out.println("Error");
        }
       //start very long if
    if (chemical.equals("Ca")) {
        System.out.println("Calcium");
    
    }
    main();
    }
    public static void main(String[] args)  {
      Chemicalcommandline client = new Chemicalcommandline();
      client.start();
    }
    }  
    

    How could I call the main so that the code does not quit after getting one input?