3

I am trying to do a practice problem where I have to write a method that takes a string of curly brackets and returns true if the brackets match up and false if they don’t. If I'm passed the empty string, it needs to return true. We can also assume that the given string can have { and } in it or be empty.

Matching has to be in the correct order of pairs like "{}" not "}{"
These are examples of brackets matching:
{}
{}{}
{{}}
{{{}{{}}}}

These are examples of the brackets not matching:
{
}{
{{}
{{}}}{}

This is what I currently have (Keep in mind I'm relatively new to coding):

public boolean bracketsMatch(String brackets)
{
    int count = 0;
    if (brackets.length() % 2 == 1){
        return false;
    }
    for(int i = 0; i < brackets.length(); i++){
        if ((brackets.charAt(i)+"") == "{"){
            count++;
        } else if ((brackets.charAt(i)+"") == "}"){
            count--;
        }
        if (count == -1){
            return false;
        }
    }
return count == 0;
}

Inputs like this }{ and {}}{}{ this still dont return the right output.

  • You should define what you mean by "matching". The string "}{" matches fine, by one definition. If you mean that they should always be in the order of "{}", and that they can enclose other "{}" pairs, then state that, and look at your program and your problem inputs again. You're determining whether there are always closing brackets in number to match opening brackets, but nothing about order or enclosure. – arcy Jan 20 '18 at 18:20
  • I suggest you learn about [Regular_Expression](https://docs.oracle.com/javase/tutorial/essential/regex/intro.html) – muneeb_ahmed Jan 20 '18 at 18:21
  • He's attempting to learn programming, and using this as a programming exercise. Regular expressions are useful in some situations, and he can solve his problem (eventually) with regular expressions, but it isn't the question he's asking. – arcy Jan 20 '18 at 18:22
  • Clarified that @arcy, Thanks – Ryan Circelli Jan 20 '18 at 18:24
  • @River I guess this is a duplicate but it is an example that might be better understood by beginners. This question can be found of CodeHS under Java Methods section as exercise 3.8.9 and might help other beginners coming from there. – Ryan Circelli Jan 20 '18 at 18:40
  • @Sotirios Delimanolis ^^ (Won't let me tag two people) – Ryan Circelli Jan 20 '18 at 18:41

1 Answers1

1

Your logic works, but the way you're comparing characters is wrong: never use == to compare string values - it tests for reference equality (whether they are the same object). Try brackets.charAt(i) == '{' instead.

Kirill Simonov
  • 8,257
  • 3
  • 18
  • 42
  • That worked, Thanks! I don't know why it previously didn't work because I was converting that to a string by adding + "". – Ryan Circelli Jan 20 '18 at 18:28
  • @Ryan Right, you converted it to a string and used `==` to compare it with another string, wich gave you false, because `==` doesn't compare string values, it compares their references. Take a look at [this post](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Kirill Simonov Jan 20 '18 at 18:30