在Java8中,使用开关大小写迭代LinkedHashSet会导致跳过匹配的参数
我正在迭代LinkedHashSet(),并使用switch捕获一些值在Java8中,使用开关大小写迭代LinkedHashSet会导致跳过匹配的参数,java,switch-statement,Java,Switch Statement,我正在迭代LinkedHashSet(),并使用switch捕获一些值 iterating through -i 5 -r 3 -t 3 -d 55.55.555.55 case "-i": int interval = Integer.parseInt(iterator.next().toString().trim()); switch (interval) { case 5: goodConfigCollector.add("\t\t* Interval:
iterating through
-i 5 -r 3 -t 3 -d 55.55.555.55
case "-i":
int interval = Integer.parseInt(iterator.next().toString().trim());
switch (interval) {
case 5:
goodConfigCollector.add("\t\t* Interval: " + interval);
break;
}
break;
case "-r":
int retries = Integer.parseInt(iterator.next().toString().trim());
switch (retries) {
case 3:
goodConfigCollector.add("\t\t* Retries: " + retries);
break;
}
break;
case "-t":
int timeout = Integer.parseInt(iterator.next().toString().trim());
switch (timeout) {
case 3:
goodConfigCollector.add("\t\t* Timeout: " + timeout);
break;
}
break;
case "-d":
String dip = iterator.next().toString().trim();
switch (dip) {
case “55.55.555.55”:
goodConfigCollector.add("\t\t* D-IP: " + dip);
break;
}
break;
to troubleshoot, I printed out each case
case "-i":
System.out.println(iterator.next().toString());
case "-r":
System.out.println(iterator.next().toString());
case "-t":
System.out.println(iterator.next().toString());
case "-d":
System.out.println(iterator.next().toString());
输出
-i -> 5 -r -> 3 -t -> -d -> 55.55.555.55
请注意,我没有得到-t的值,这解释了为什么我首先会收到错误
error when -r and -t value are the same:
Caused by: java.lang.NumberFormatException: For input string: "-d"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
注意,我的字符串的值为r=3,t=3。因此,如果我将-t的值更改为不同的值,如7或其他任何值,则重新打印。我得到了t的值
将我的字符串更改为-I5-R3-T7-D55.55.555.55
Output now is equal to
-i --> 5 -r --> 3 -t --> 7 -d --> 55.55.555.55
错误信息非常清楚:
java.lang.NumberFormatException: For input string: "-d"
似乎在代码中的某个地方,您试图将字符串
-d
转换为数字我发现了问题:我的字符串有重复的值
String
-r 3 -t 3 -d 55.555.555
因此,当我将它们存储到LinkedHashSet中时,它们被存储为-r3-t-d55.555.555,将重复的3放在-t之后
当我的情况是“-t”;匹配后,我尝试将.next()转换为整数。在本例中,next()等于-d。因此我犯了错误
So the solution is:
instead of using LinkedHashSet I should be using a list instead
List<String> set1 = new ArrayList<>();
as it allows duplicates. Thanks all for your help.
Side Note: I am not sure why I got -4 for this question.
But anyways whoever gave me the -4 points, thanks for looking.
因此解决方案是:
我不应该使用LinkedHashSet,而应该使用列表
List set1=new ArrayList();
因为它允许复制。谢谢大家的帮助。
旁注:我不知道为什么这个问题我得了-4分。
但不管怎样,不管是谁给了我-4分,谢谢你的关注。
您是否尝试打印“[”+超时+“]”以验证其值?您需要告诉您在使用哪个java版本时出现了哪些错误?它将在java 7及以上版本中工作。@Rustam:他说的是运行时行为,如果不是一个字符串,他不会走这么远,他正在打开MainController.java中的第120行是什么?甚至不是“某处”,而是在ciacv.ReadFile.testScan(ReadFile.java:2437)
。哦,看看你们班上的第2437行,这就是你们的问题所在。不是开关箱。格菲,你说得对,是-d出了点问题。。。我更新了我的问题以反映当前问题。