Creating commands for a terminal application in Java

I am new to programming and I am creating an application that only works on the command line. I found that I can use BufferedReader to read 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(); 

Now I'm trying to find a way to create different β€œcommands” that you can use by simply typing them at the command line. But these commands may need to be used several times. Should I use some kind of command design template with a huge switch statement (this doesn't seem right to me)? I would like to avoid using an extra library .

Can anyone with a lot of experience that I'm trying to help me?

+7
java command-line shell
source share
3 answers

You can try something like this:

 public static void main(String[] args) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String input = ""; try { while (!input.equalsIgnoreCase("stop")) { showMenu(); input = in.readLine(); if(input.equals("1")) { //do something } else if(input.equals("2")) { //do something else } else if(input.equals("3")) { // do something else } } } catch (IOException e) { e.printStackTrace(); } } public static void showMenu() { System.out.println("Enter 1, 2, 3, or \"stop\" to exit"); } 

Good practice is to store variables in the lower case.

I would also say that !Input.equalsIgnoreCase("stop") much more readable than Input.equalsIgnoreCase("stop") == false , although both are logically equivalent.

+4
source share

Start with an infinite while loop for the program.

 while(true) { //code here } 

add if statements to detect commands and some other things.

 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Example { public static void main(String[] args) throws IOException { while (true) { BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String command = ""; command = in.readLine(); // These are your commands. if (command.equalsIgnoreCase("FirstCommand")) { System.out.println("My first Command!"); } else if (command.equalsIgnoreCase("SecondCommand") || command.equals("AlternativeSecondCommand")) { System.out.println("My second command!"); } else if (command.equalsIgnoreCase("ThirdCommand" + "ParameterOne")) { System.out.println("Parameter one of command three!"); } else if (command.equalsIgnoreCase("Stop") || command.equalsIgnoreCase("Exit") || command.equalsIgnoreCase("Quit")) { System.exit(0); } else { System.out.println("Error: Unknown Command"); } } } } 

just add if statements for more commands.

It works great and its very easy for beginners. Give it a try!

+1
source share

If you just read the program parameters, you can simply add them for calling the Java application and access them through the args argument of your main method. And then you can scroll through the array and look for flags that your program accepts.

0
source share

All Articles