简体   繁体   中英

How can I receive an input from my Terminal and use it properly

My task is to receive a command via the Terminal like "insert number" and call an insert methode using the number.

My methode is working. This is my code snippet:

String command = Terminal.readLine();
    while (command != null) {

        switch (command) {
        case "insert number":
            String[] split = command.split(" ");
            linkedTuple.insert(Integer.parseInt(split[1]));
            break;

only a part of my complete code. My problem is if I use case "insert number" it will only work if I really would write insert number in my terminal but instead I want to write for example insert 3 to insert the number 3 but how can I call that in my switch case? My Terminal is working because a command like quit which is quitting my Application is working.

Thank you!

Edit: For clarity my main methode:

  public static void main(String[] args) {

    int[] tuple = { 1 };
    LinkedNaturalNumberTuple linkedTuple = new LinkedNaturalNumberTuple(
            tuple);


    String command = Terminal.readLine();
    String[] split = command.split(" ");
    while (command != null) {

        switch (split[0]) {
        case "insert":

            linkedTuple.insert(Integer.parseInt(split[1]));
            break;
        case "remove":

            Terminal.printLine(""
                    + linkedTuple.remove(Integer.parseInt(split[1])));
            break;
        case "swap":

            if (!linkedTuple.swap(Integer.parseInt(split[1]),
                    Integer.parseInt(split[2]))) {
                Terminal.printLine("Error, your numbers are invalid please try again!");
            }
            break;
        case "min":
            if (linkedTuple.min() == -1) {
                Terminal.printLine("Error, your tuple is empty, use insert number to insert a number!");
            } else {
                Terminal.printLine("" + linkedTuple.min());
            }
            break;
        case "max":
            if (linkedTuple.max() == -1) {
                Terminal.printLine("Error, your tuple is empty, use insert number to insert a number!");
            } else {
                Terminal.printLine("" + linkedTuple.max());
            }
            break;
        case "info":
            Terminal.printLine(linkedTuple.toString());
            break;
        case "quit":
            System.exit(1);
            break;

        }
        command = Terminal.readLine();
    }

}

If I enter a command and then want to enter another one the first one will be called instead. for example: info then my tuple is printed insert 3 my tuple is printed quit my tuple is printed etc

Use something like:

String[] array = "insert number".split(" ");
....
switch (array[0]) {
case "insert":

In addition to above you need to swap the order of statement from

String command = Terminal.readLine();
String[] split = command.split(" ");
while (command != null) {

To

String command = Terminal.readLine();
String[] split;
while (command != null) {
    command = Terminal.readLine();
    split = command.split(" ");

So that you could loop in and ask for user input every time and take split from new command that user has entered.

Answer after your edit:

At the end of your loop you do

command = Terminal.readLine();

But you don't split the new command. As a result, your switch statement still looks at the old split.

So you need to change the end of the loop to:

command = Terminal.readLine();
split = command.split(" ");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM