Answer the question
In order to leave comments, you need to log in
What's wrong with Java "foreach"?
I wanted to pass a test task for qualification, the task is simple, check the brackets.
I made it in IDEA, it works,
public static boolean isValid(String braces) {
ArrayDeque stack = new ArrayDeque ();
for(char ch: braces.toCharArray()){
switch (ch) {
case '(': stack.add(0); break;
case '[':stack.add(1); break;
case '{': stack.add(2); break;
case ')':{if(stack.isEmpty() || (Integer) stack.pollLast()!=0) return false; else break;}
case ']':{if(stack.isEmpty() || (Integer) stack.pollLast()!=1) return false; else break;}
case '}':{if(stack.isEmpty() || (Integer) stack.pollLast()!=2) return false; else break;}
default: break;
}
}
return stack.isEmpty() ? true: false;
}
Compiling...
Main.java:12: error: cannot find symbol
for(char ch: braces.toCharArray()){
^
symbol: variable braces
location: class Solution
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
Test Result: Failed
Answer the question
In order to leave comments, you need to log in
But again, I come back to the idea that maybe I just don’t know something, and in vain so on a well-known service ...
Therefore, if someone can shed light on this mystery,
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question