0

I'm writing a code to check for balanced brackets but I'm having a problem, probably with Java syntax. In the following code:

String s0 = "{{[[(()[])]{()}]}}}";
String[] s1 = s0.split("");
if (s1[0] == "{")
    System.out.println("Ok");
else
    System.out.println("Nope");

Why the comparison

s1[0] == "{"

is not true?

  • 1
    Does this `s0.split("");` even work? – Scary Wombat Oct 25 '16 at 03:00
  • 1
    @ScaryWombat that may depend on Java version: http://stackoverflow.com/questions/22718744/why-in-java-8-split-sometimes-removes-empty-strings-at-start-of-result-array – Pshemo Oct 25 '16 at 03:01
  • 4
    Why split on empty string? You could use `toCharArray` if you want an array of character, which you **can** use `==` on – OneCricketeer Oct 25 '16 at 03:01
  • @cricket_007 - Don't you know? Once you have learned about regexes, you never need to use any other tools. Ever. :-) – Stephen C Oct 25 '16 at 03:02
  • seems like split creates strings by new String("}"),so == returns false because its a new object.but you can compere it with equals() method – sampathpremarathna Oct 25 '16 at 03:38

0 Answers0