Reputation: 516
Here I have my terminal project, and inside the terminal, I can type "create", which will take me to the create prompt, where I can create a program. My problem right now is the fact that I can't get back to the Main class (Where I can select a command to run). I had the idea of trying to use the System.exit(0); but, as I didn't realise, it just kills the entire program. If anyone is able to help me, my file is below. I can post any other files if requested.
import java.util.*;
import java.io.*;
public class commandCreate {
boolean _active = true;
String _username = System.getProperty("user.name").toLowerCase();
String _os = System.getProperty("os.name").trim().toLowerCase();
String fileName, create, option;
public commandCreate() {
try {
while(_active) {
System.out.print(_username + "@" + _os + ":~/create$ ");
Scanner kbd = new Scanner(System.in);
String userLine = kbd.nextLine();
if(java.util.regex.Pattern.matches(".*\\S\\s+\\S.*", userLine)) {
Scanner read = new Scanner(userLine);
option = read.next();
fileName = read.next();
}
FileWriter create = new FileWriter(new File("Created Files/" + fileName + ".java"));
if(userLine.equals(option + " " + fileName)) {
if(option.equals("-a")) {
// Option = -a, creates standard file with main class.
create.write("public class " + fileName + " {\n");
create.write(" public static void main(String[] args) {\n");
create.write(" System.out.println(\"Welcome to your new program!\");\n");
create.write(" }\n");
create.write("}");
} else if(option.equals("-c")) {
// Option = -c , creates standard file with overloaded constructor & main class.
create.write("public class " + fileName + " {\n");
create.write(" public " + fileName + "() {\n");
create.write(" System.out.println(\"Welcome to your new program!\");\n");
create.write(" }\n");
create.write("\n");
create.write(" public static void main(String[] args) {\n");
create.write(" new " + fileName + "();\n");
create.write(" }\n");
create.write("}");
} else if(option.equals("-j")) {
// Option = -j, creates GUI within constructor w/ single JLabel.
create.write("import javax.swing.*;\n");
create.write("import java.awt.*;\n");
create.write("import java.awt.event.*;\n");
create.write("\n");
create.write("public class " + fileName + " extends JFrame {\n");
create.write(" private static final int HEIGHT = 50;\n");
create.write(" private static final int WIDTH = 400;\n");
create.write("\n");
create.write(" private JLabel welcomeJ;\n");
create.write("\n");
create.write(" public " + fileName + "() {\n");
create.write(" super(\"Welcome to your program - " + fileName + "\");\n");
create.write(" Container pane = getContentPane();\n");
create.write(" setLayout(new FlowLayout());\n");
create.write("\n");
create.write(" welcomeJ = new JLabel(\"Welcome To Your Program!\", SwingConstants.CENTER);\n");
create.write("\n");
create.write(" pane.add(welcomeJ);\n");
create.write("\n");
create.write(" setSize(WIDTH, HEIGHT);\n");
create.write(" setVisible(true);\n");
create.write(" setResizable(false);\n");
create.write(" setDefaultCloseOperation(EXIT_ON_CLOSE);\n");
create.write(" }\n");
create.write("\n");
create.write(" public static void main(String[] args) {\n");
create.write(" new " + fileName + "();\n");
create.write(" }\n");
create.write("}");
}
} else if(userLine.equalsIgnoreCase("help")) {
System.out.println("Commands");
System.out.println(" Syntax: [-option] [filename]");
System.out.println(" -a [filename] [Program: main class]");
System.out.println(" -c [filename] [Program: overloaded constructor, main class]");
System.out.println(" -j [filename] [Program: GUI: overloaded constructor, main class]");
} else if(userLine.equalsIgnoreCase("exit")) {
System.exit(0);
} else {
System.out.println("Error in syntax. Please review the \"help\" menu");
}
create.close();
}
} catch(IOException e) {
System.out.println("There was an error: " + e);
} catch(InputMismatchException ex) {
System.out.println("There was an error: " + ex);
}
}
public static void main(String[] args) {
new commandCreate();
}
}
Upvotes: 1
Views: 3626
Reputation: 335
Your problem seems to be that you are stuck in an infinite while loop, there is no condition that sets the value _active
to false.
} else if(userLine.equalsIgnoreCase("exit")) {
System.exit(0);
} else {
with
} else if(userLine.equalsIgnoreCase("exit")) {
_active = false;
} else {
That pretty much solves the problem of not being able exit. A return;
statement would work equally well. I think Exceptions would be overkill in this particular instance.
On a side note (and something that most people seem to have pointed out), I would put the code in it's own method, run()
for instance, and then use the call new commandCreate().run()
in your main
method.
Upvotes: 0
Reputation: 718778
The simple answer is to get the commandCreate
constructor to return, or throw / propagate an exception. Indeed, I think this will happen already if the user enters an EOF.
(There are numerous other things wrong with your code, but it is probably better if you figure that out for yourself. I will point out however, that "commandCreate" or "CommandCreate" is a really poor choice for a class name. A class name is typically a noun.)
Upvotes: 1