Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7640593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:42:33+00:00 2026-05-31T08:42:33+00:00

I posted a thread on here a few days ago relating to this file,

  • 0

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:42:35+00:00Added an answer on May 31, 2026 at 8:42 am

    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 _active variables in Main and commandCreate (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. Main and commandCreate both handle the "exit" command, for example. From what I assume you want to achieve, only Main (or better a specialized CommandProcessor) should handle the global commands. Thus "exit" should either have a different meaning while executing commandCreate (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 Main should check their state and be the only one to stop the application and maybe do some cleanup first. If you keep the "exit" command inside commandCreate it might just notify the command processor (Main in 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 Main ever, thus the loop in the constructor (which as @Andreas_D already pointed out is bad already) is never executed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found some source code in this thread posted by Rex Logan here on
A few days ago I posted a thread asking on how to find the
Weeks ago, I posted this thread regarding problems I was having with AVAssetWriter: AVAssetWriter
I originally posted this at the Amazon SES forums here: https://forums.aws.amazon.com/thread.jspa?threadID=74561&tstart=0 But since the
I posted a thread about this earlier and have made some progress but now
Someone posted a great little function here the other day that separated the full
I posted this question: https://stackoverflow.com/questions/418597/java-and-net-for-php-programmer and the answers I was given didn't really help
(Question also posted here: http://www.google.com/support/forum/p/Android+Market/thread?tid=68dc1f694537c7dc&hl=en ) http://developer.android.com/guide/publishing/publishing.html recommends searching for apps by Developer Name
So here's some code on http://groups.google.com/group/android-developers/browse_thread/thread/16a2216b39513674 in which the poster writes that he's shortened
I posted this is the thread which discussed about request_threaded_irq but I did not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.