I'm new to programming, and I'm making an app that only runs in the command-line. I found that I could use a BufferedReader to read the inputs from the command-line.
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String Input = "";
while (Input.equalsIgnoreCase("Stop") == false) {
Input = in.readLine();
//Here comes the tricky part
}
in.close();
What I'm trying to do now is to find a way to create different "commands" that you could use just by typing them in the command-line. But these commands might have to be used multiple times. Do I have to use some kind of Command design pattern with a huge switch statement (that doesn't seem right to me)? I'd like to avoid using an extra library.
Can someone with a bit more experience that me try to help me?
If it's just about reading the program parameters you can just add them behind the Java application call and access them through your args argument of your main method. And then you can loop through the array and search for the flags your program accepts.
You could try something like this:
It is good practice to keep your
variables
lower cased.I would also say that
!Input.equalsIgnoreCase("stop")
is much more readable thanInput.equalsIgnoreCase("stop") == false
although both are logically equivalent.