title: If else vs switch with stack logic
body: <p>I'm learning about stacks by writing a program that validates bracket syntax. If I input <code>(Baller)</code> it should give me a positive result. If I have <code>(Baller()</code> it should give me a negative result. Essentially the application checks if the user has correctly used ( ) , { } and []. </p>
  
  <ul>
  <li>If I encounter a ( , { , or [ character I will add it to the
  character on the stack.</li>
  <li>If I encounter a ) } ] sign I will delete a character from the stack.</li>
  <li>If the text contains an odd number of parentheses or brackets are not
  contiguous ( for example, ( and ] is not continuous ) , an error<br>
  message is printed.</li>
  </ul>
  
  <p>So I have done this half in a if else statement but I was thinking that it should be easier to make it in a switch statement and also be a good learning experience. </p>
  
  <p>So what I have done in the switch statement is this:</p>
  
  <pre><code>public class Input {
      public static void main(String[] args) {
          Stack stack = new Stack();
          String str;
          str = JOptionPane.showInputDialog("Text to parse: ");
          char arr[] = str.toCharArray();
          System.out.print(str);
          System.out.println();
          System.out.println();
  
          for(char c : arr) {
              switch(c) {
  
              case '{':
                  stack.Push(c);
                  System.out.print(stack.firstNode.getData());
                  break;
              case '(':
                  stack.Push(c);
                  System.out.print(stack.firstNode.getData());
                  break;
              case '[':
                  stack.Push(c);
                  System.out.print(stack.firstNode.getData());
                  break;
  
              case '}':
                  c = (Character) stack.Peek(); //&lt;-- Edited for @Jimmy
                  if( c != '{') {
                      System.out.println("  Syntax ERROR");
                  }
              case ']':
                  if( c != '[') {
                      System.out.println("  Syntax ERROR");
                  }   
              case ')':
                  if( c != '(') {
                      System.out.println("  Syntax ERROR");
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>But now I'm getting a problem that if I just add a right side bracket it will just remove because I have a pop. I tried to make this in a if-else statement which will end in a if-statement like this:</p>
  
  <pre><code>if(first == '(' &amp;&amp; (current == '}' || current == ']')) {
  if first == '{' &amp;&amp; (current == ']' || current == ')')) {
  //and so on
  </code></pre>
  
  <p>How could I turn this into a switch case? Is this a bad idea? </p>
  
  <p>What I know is there isn't really a problem on my left side bracket but there is on the right ones. </p>
  
  <p>EDIT: HOW THE CODE LOOKS LIKE RIGHT NOW</p>
  
  <pre><code>import javax.swing.JOptionPane;
  
  
  
  public class Input {
      public static void main(String[] args) {
          Stack stack = new Stack();
          String str;
          str = JOptionPane.showInputDialog("Text to parse: ");
          char arr[] = str.toCharArray();
          System.out.print(str);
          System.out.println();
          System.out.println();
  
  
          for(char c : arr) {
  
              switch(c) {
  
              case '{':
                  stack.Push(c);
                  break;
              case '(':
                  stack.Push(c);
                  break;
              case '[':
                  stack.Push(c);
                  break;
  
              case '}':
                  if(stack.isEmpty() || (Character) stack.Pop() != '{') {
                      System.out.println("  Syntax ERROR");
                  }
                  break;
              case ']':
                  if(stack.isEmpty() || (Character) stack.Pop() != '[') {
                      System.out.println("  Syntax ERROR");
                  }
                  break;
              case ')':
                  if(stack.isEmpty() || (Character) stack.Pop() != '(') {
                      System.out.println("  Syntax ERROR");
                  }
                  break;
              }
          } if(!stack.isEmpty()) {
              System.out.println("  Syntax ERROR");
          }
      }
  }
  </code></pre>