【发布时间】:2013-07-09 15:55:33
【问题描述】:
(对不起,这个奇怪的标题,但我无法弄清楚究竟是什么问题)
下面的代码应该首先从命令行获取一个字符串(有效),然后输入被拆分(也可以完美地工作;我通过在 if/else 之前打印两个字符串进行检查,正如您在部分中看到的那样我再次注释掉)然后它应该检查分割字符串的第一部分是什么。例如,如果它等于“tweet”,它应该使用 Tweet 方法处理。
但不知何故,它并没有做到这一点。它总是执行 else 语句...
Scanner sc = new Scanner(System.in);
System.out.print("> ");
String input = sc.nextLine();
String[] splitString = input.split(" ");
if(splitString.length != 2){ throw new IllegalArgumentException(); }
String command = splitString[0];
String value = splitString[1];
/*System.out.print(command);
System.out.print(value);*/
if(command == "tweet") { Tweet(value); }
else if(command == "help") { ShowHelp(); }
else { System.out.println("Command "+command+" not found."); }
我尝试输入“tweet asdf”,但它返回了
> tweet asdf
Command tweet not found.
我做错了什么?我很困惑D:
【问题讨论】:
标签: java string if-statement