【发布时间】:2018-09-12 17:20:43
【问题描述】:
我有以下代码,除了命令行参数外,每次我写"Insertion"它都不会进入if语句,所以输出将是"Algorithm not found. Use: [ Insertion | Merge ]"
public static void main(String[] args) throws IOException, InsertionAndMergeException, Exception {
if( args.length < 2 ) {
System.out.println("Use: <path> <algorithm> ");
System.exit(1);
}
if(args[1] == "Insertion" || args[1] == "Merge"){
testWithComparisonFunction(args[0], args[1], new RecordComparatorIntField());
}else
System.out.println("Algorithm not found. Use: [ Insertion | Merge ]");
}
我在命令行中输入这个,我做错了什么?
java insertionandmergeusagejava/InsertionAndMer
geUsage "/home/zenoraiser/Scrivania/Università/Secondo Anno/Algoritmi/1718/LAB/Progetto/Integers.txt" "Insertion"
【问题讨论】:
-
您比较错误的字符串。 == 是引用比较,需要按值比较,所以改用equals方法。只需更改此: if(args[1] == "Insertion" || args[1] == "Merge") 如下: if ("Insertion".equals(args[1]) || "Merge".equals (args[1]))
-
用
return;替换System.exit(1);
标签: java shell command-line args