I posted a thread on here a few days ago relating to this file, it got some good answers/ideas that I am still yet to implement because I have been working and drinking and haven’t had time to get around to it yet (lol). Anyway, the reason I am re-posting is because I kind of goofed up the question. The problem I’m having is with my while(_active) loop, I’ve wrapped my Main class and my commandCreate class with this for an easy file exit, just by changing the boolean to false. But, in my commandCreate class the boolean is set to false, and while it is false, I can’t go into the Create section, because it won’t allow me to, it just keeps going back to Main, where-as if I change the boolean to true, it will automatically take me straight to the Create section, completely skipping the Main class, even though I launch into the Main class. If anyone is able to help me, both my Main and commandCreate classes are below.
Do not suggest for me to change my commandCreate to a method, and to change anything around right now, I just want to get this fixed
Main.java
import java.io.*;
import java.util.*;
public class Main extends API {
private boolean _active = true;
String _username = System.getProperty("user.name").toLowerCase();
String _os = System.getProperty("os.name").trim().toLowerCase();
CommandCreate create = new CommandCreate();
public Main() {
try {
while(_active) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
print(username() + "@" + os() + ":~$ ");
String command = br.readLine();
if(command.equalsIgnoreCase("create")) {
new CommandCreate();
/*} else if(command.equals("compile")) {
new CommandCompile();*/
} else if(command.equalsIgnoreCase("help")) {
println("Commands");
println(" create - Creates .java files, does not compile.");
//println(" compile - Creates .java files, compiles on creation.");
println(" exit - Exits program");
println(" help - Shows help documentation.");
} else if(command.equalsIgnoreCase("exit")) {
/*print("Are you sure you want to exit? (Y/N) ");
String exit = br.readLine();
if(exit.equalsIgnoreCase("y")) {
exit();*/
_active = false;
/*} else {
println("Cancelled!");
}*/
} else if(command.isEmpty()) {
} else {
println("\"" + command + "\" does not exist. Please review the \"help\" menu");
}
}
} catch(IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
new Main();
}
}
commandCreate.java
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();
}
}
I’m not sure I correctly understand your question, but there are some things that really seem odd to me and I also have the impression you got a misconception there.
First of all, the
_activevariables inMainandcommandCreate(btw, according to the convention class names should start with capital letters) are totally independent and you’d have to manually synchronize them if you want them to depend on each other.Second you seem to handle some commands in two places, i.e.
MainandcommandCreateboth handle the"exit"command, for example. From what I assume you want to achieve, onlyMain(or better a specializedCommandProcessor) should handle the global commands. Thus"exit"should either have a different meaning while executingcommandCreate(it should not stop the application itself) or you might want to introcude another command to indicate the"create"command should be finished.However you handle the commands the loop in
Mainshould check their state and be the only one to stop the application and maybe do some cleanup first. If you keep the"exit"command insidecommandCreateit might just notify the command processor (Mainin your case) in a way that’s appropriate (listener callbacks, setting some flags etc.).Edit:
An additional observation: you’re not creating any instance of
Mainever, thus the loop in the constructor (which as @Andreas_D already pointed out is bad already) is never executed.