java - Trying to get the code to check if there are even brackets -


i trying come code scan string , check see if there number of open , closing brackets on each line. if so, return true. (excuse me incorrectness in formatting not examples take shape unless identified code)

{}    // code return true     {{}} {}{} {{{}{{}}}}   }     // code return false {} }}{ {{{}{} 

what tried far:

    public boolean bracketsmatch(string brackets) {   int lb = 0;   int rb = 0;   int = 0;   while (brackets.charat(i) == '{' || brackets.charat(i) == '}' || brackets.charat(i) == '')   {       if (brackets.charat(i) == '{')       {         lb += 1;        }        if (brackets.charat(i) == '}')       {           rb += 1;       }        if (brackets.charat(i) == '')       {           if (lb / rb == 2)           {               // possible code scan next line next line?               // need statement here ^^ before can place if statement below             if (bracket.charat(i + 1) == '')             {               return true;              }           }              else            {               return false;           }       }       i++    } } 

i apologize in advance experienced programmers inefficient nightmare. relatively new programming in general. attempted have code check number of left brackets (lb) , right brackets (rb). whenever code came empty string, divide lb rb. if code did not equal 2, code return false. have more dozen errors in code, wondering if there way have code go onto next line scan next set of brackets. in advance.

edit 1:

public boolean bracketsmatch(string brackets) {   int balance = 0;   (int = 0; < brackets.length(); i++)   {       char value = brackets.charat(i);       if (value == '{')       {         balance += 1;        }        else if (value == '}')       {           balance -= 1;       }    }    if (balance != 0)   {       return false;   }    else   {       return true;   } } 

this won't compile, '' invalid character literal:

if (brackets.charat(i + 1) == '') 

and current approach of counting opening , closing brackets, , checking value of lb / rb won't yield right result.

you don't need count right brackets. need count open brackets, , reduce count closed.

here's sketch of algorithm can use, hope not spoil exercise:

  1. for each character in string
  2. if it's open bracket, increment count
  3. if it's close bracket
    • if open count 0, there's nothing close, not balanced, can stop
    • decrement count
  4. after characters, if open count 0, brackets balanced

as additional code review note, bad in many ways:

  if (brackets.charat(i) == '{') {     // ...   }    if (brackets.charat(i) == '}') {     // ...   } 

what's bad:

  • calling brackets.charat(i) repeatedly unnecessary if result same. call once , save result in variable.
  • the 2 if conditions exclusive: if first true, second won't true, it's pointless evaluate it. second condition should if else instead of if. , instead of if-else chain, switch more interesting here.
  • instead of calling string brackets, better call more general. if actual input "{something}"? contains more brackets, algorithm work same. calling brackets misleading.

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -