How to call main from inside class
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.pri开发者_开发百科ntln("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?
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.
No you cant invoke main this way. Use a while loop with some form of terminal condition in the input reading.
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();
}
}
Here is another example that demonstrates a couple good class design practices
A class constructor
Use of the main method to instantiate the class
Use of a boolean conditional loop, with a statement about how to end the loop
import java.io.*;
public class Chemicalcommandline {
//class constructor
public Chemicalcommandline(){
start();
}
public static void start() {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String chemical = null;
Boolean done = false;
while(!done){
String instructions = "This program "
+ ""
+ ""
+ ""
+ "";
System.out.println(instructions);
System.out.println("Chemical Sign: , enter exit to end");
try {
chemical = reader.readLine();
if(chemical.equals("exit")){
done = true;
}
}catch(IOException ioe) {
System.out.println("Error");
}
if (chemical.equals("Ca")) {
System.out.println("Calcium");
}else{
System.out.println("not sure what that is...");
}
}// end while
}//end method start
public static void main(String[] args){
new Chemicalcommandline();
}
}//end class Chemicalcommandline
精彩评论