标题翻译
Why isn't .equals recognizing my variable as the same string that I'm passing it?
问题
以下是翻译好的内容:
这个小函数的目的是检查相邻的括号和方括号是否匹配。我觉得它应该能工作,我已经尝试过几种不同的方式,但是我无法弄清楚如何检查下一个字符是否是我期望的字符。
class Parenths {
public boolean isValid(String s) {
char[] parens = s.toCharArray();
if (parens.length == 0) return true;
for (int i = 0; i < parens.length; i+=2) {
String curr = String.valueOf(parens[i]);
String next = String.valueOf(parens[i+1]);
// System.out.println(next.equals(")"); --------> false
// System.out.println(Object.equals(next, ")")); ----> error
switch (curr) {
case "(": if (!next.equals(")")) return false;
case "{": if (!next.equals("}")) return false;
case "[": if (!next.equals("]")) return false;
}
}
return true;
}
}
你可以看到我打印的用于调试的行,似乎 .equals
不是在这里使用的正确方法?有人可以解释一下为什么这不起作用吗?
附:我意识到我不需要将字符串转换为字符数组来比较元素,所以除非这是唯一的修复方法,否则请不要指出这一点给我。
英文翻译
So this little function is supposed to check if parentheses and brackets are matched next to each other. I feel like it should work and I've tried it a few different ways but I can't figure out how to check if my next char is what I expect it to be.
class Parenths {
public boolean isValid(String s) {
char[] parens = s.toCharArray();
if (parens.length == 0) return true;
for (int i = 0; i < parens.length; i+=2) {
String curr= String.valueOf(parens[i]);
String next = String.valueOf(parens[i+1]);
// System.out.println(next.equals(")"); --------> false
// System.out.println(Object.equals(next, ")")); ----> error
switch (curr) {
case "(": if (!next.equals(")")) return false;
case "{": if (!next.equals("}")) return false;
case "[": if (!next.equals("]")) return false;
}
}
return true;
}
}
You can see the lines I printed to debug and it seems that .equals is not the right thing to use here? Can anyone explain why this isn't working?
PS. I realize I don't have to convert the string to a char array to compare elements, so unless that's the only fix, please don't point that out to me.
答案1
得分: 0
没有经过测试,但似乎是一个“穿透”的问题。<s>尝试将if (boolean) return boolean
替换为return boolean
,这应该能解决问题。</s>
问题是你在case
结束时没有加上break
语句,因此,例如,如果第一个case
为真,它不会停止执行并继续执行第二个测试,而第二个测试会返回假。如果你将条件语句改为直接返回,就不会出现这个问题。
编辑:抱歉,我读得太快了。这样做会破坏你的循环。实际上,你必须在每个case
的末尾加上break
语句。
case "(": if (!next.equals(")")) return false; break;
case "{": if (!next.equals("}")) return false; break;
case "[": if (!next.equals("]")) return false; break;
英文翻译
Not tested, but it seems to be a problem of fall through. <s>Try to replace if (boolean) return boolean
with return boolean
, this should do the trick.</s>
The problem is that you don't have a break
at the end of the cases, so if, for example, your first case is true, it will not stop execution and execute the 2nd test, which will be false. If you change your conditional statements to a direct return, you will not have this problem.
EDIT: Sorry, I read too quickly. Doing so will break your loop. Actually, you have to add a break
at the end of the cases.
case "(": if (!next.equals(")")) return false; break;
case "{": if (!next.equals("}")) return false; break;
case "[": if (!next.equals("]")) return false; break;
答案2
得分: 0
首先,在每个 case 后面添加 `break;` 语句,这很重要,以便停止匹配其他的 case:
switch (curr) {
case "(": if (!next.equals(")")) return false;
break;
case "{": if (!next.equals("}")) return false;
break;
case "[": if (!next.equals("]")) return false;
break;
}
其次,你的代码一开始不支持闭合括号的情况,你需要添加一个默认的 case:
switch (curr) {
case "(": if (!next.equals(")")) return false;
break;
case "{": if (!next.equals("}")) return false;
break;
case "[": if (!next.equals("]")) return false;
break;
default :
break;
}
return true;
另外,你需要确保下一个元素不为 null,然后再进行比较,同时不要将索引递增2,因为你需要使用一个元素的字符串,这也是你遇到错误的原因:
public static boolean isValid(String s) {
char[] parens = s.toCharArray();
if (parens.length == 0) return true;
for (int i = 0; i < parens.length; i++) {
String curr = String.valueOf(parens[i]);
String next = "";
try {
next = String.valueOf(parens[i+1]);
switch (curr) {
case "(": if (!next.equals(")")) return false;
break;
case "{": if (!next.equals("}")) return false;
break;
case "[": if (!next.equals("]")) return false;
break;
default:
break;
}
return true;
} catch(Exception e) {}
}
return false;
}
-----------------------------------
- 测试:
System.out.println(isValid("()"));
// 输出:true
System.out.println(isValid("("));
// 输出:false
英文翻译
First , you have to add break;
after the cases its important to stop seeing the cases
switch (curr) {
case "(": if (!next.equals(")")) return false;
break;
case "{": if (!next.equals("}")) return false;
break;
case "[": if (!next.equals("]")) return false;
break;
}
Secondly , your code doesnt support the confrotation of a closing patenthesis at first , you have to add a default case
switch (curr) {
case "(": if (!next.equals(")")) return false;
break;
case "{": if (!next.equals("}")) return false;
break;
case "[": if (!next.equals("]")) return false;
break;
default :
break;
}
return true;
Also , you have to make sure the next element is not null before comparing to it , and dont increment with 2 , you give a String with a one element and that's why you get the error
public static boolean isValid(String s) {
char[] parens = s.toCharArray();
if (parens.length == 0) return true;
for (int i = 0; i < parens.length; i++) {
String curr= String.valueOf(parens[i]);
String next = "";
try {
next = String.valueOf(parens[i+1]);
switch (curr) {
case "(": if (!next.equals(")")) return false;
break;
case "{": if (!next.equals("}")) return false;
break;
case "[": if (!next.equals("]")) return false;
break;
default :
break;
}
return true;
}catch(Exception e) {}
}
return false;
}
-
Test :
System.out.println(isValid("()")); // Output : true System.out.println(isValid("(")); // Output : false
专注分享java语言的经验与见解,让所有开发者获益!
评论