0

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

4
  • you wrote String inside a switch() and it doesn't give you error .? can you please confirm.? Commented Dec 14, 2014 at 9:21
  • no its not giving me an error lol Commented Dec 14, 2014 at 9:23
  • @Saif you can use string since jdk 7 in switch. Commented Dec 14, 2014 at 9:25
  • ohhh!!! they put it in JDK 7. Thanks to your question. yah i just googled it .. @almasshaikh and happy . Commented Dec 14, 2014 at 9:25

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Then I would rather use String[] array = command.split(" "); switch(array[0]){ case "insert": because I also use remove number and stuff like that^^ thanks that should fix it
well but if I implement that and then run my application in eclipse and type in my terminal "info" (returning my current numbers array(standard: {1})) it is printing in 1 but if I know enter insert 3 it is still printing in a 1(like with my info command) if I now rerun it and start with insert 3 instead of info and then call info again it does nothing (Updated main thread)
paste you whole code post modification in the question please?
if I call command = Terminal.readLine(); at the beginning of my while loop I can delete it at the ending? am I right? I have to call it at the ending and not at the beginning of my while loop because If I call it at the beginning I have to double enter a command at the beginning (tested it right now)
No you need to ask user input every time isn't it so you get new command always?
|
0

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(" ");

4 Comments

I can expect my commands to be: "command number" with only one space between(is told in my task)(updated main thread)
OK, then you can split by a single space if you want. I think giving flexibility is better, but you can always reduce the level of the code if you want to.
We are kinda restricted to what we are asked for :/ Not really allowed to "do more"
OK, this is all irrelevant because you now edited your question and your problem is completely different than what you wrote at first, so I'm going to change my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.