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
Stringinside aswitch()and it doesn't give you error .? can you please confirm.?JDK 7. Thanks to your question. yah i just googled it .. @almasshaikh and happy .