import java.util.*;

public class MathsParser
{
    public MathsParser (boolean debug)
    {
        _debug = debug;
    }

    /**
     * All numbers within expressions are single digit so we can continue to use them as
     * characters.
     * 
     * Handle different precedence  to part 1.
     */

    public long parse (Vector<String> data)
    {
        long finalResult = 0L;

        /*
         * Since + has precedence, we need to unwind the entire
         * equation and compute those values first before doing *
         */

        for (int i = 0; i < data.size(); i++)
        {
            String currentLine = data.elementAt(i);
            char[] lineArray = currentLine.trim().replaceAll("\\s", "").toCharArray(); // remove all spaces
            Vector<Character> unrolled = new Vector<Character>();
            Stack<Character> nested = new Stack<Character>();

            for (int j = 0; j < lineArray.length; j++)
            {
                switch (lineArray[j])
                {
                    case Tokens.OPEN_BRACE:
                    {
                        nested.push(Tokens.OPEN_BRACE);
                    }
                    break;
                    case Tokens.CLOSE_BRACE:
                    {
                        boolean done = false;

                        while (!done && !nested.isEmpty())
                        {
                            Character c = nested.pop();

                            if (c != Tokens.OPEN_BRACE)
                                unrolled.add(c);
                            else
                                done = true;
                        }

                        nested.pop();
                    }
                    break;
                    case Tokens.PLUS:
                    {
                        System.out.println("nested "+nested);
                        System.out.println("unrolled "+unrolled);

                        System.out.println("character "+Tokens.PLUS);

                        while (!nested.isEmpty() && nested.peek() != Tokens.OPEN_BRACE)
                        {
                            System.out.println("peeked "+nested.peek());
          
                            if (nested.peek() == Tokens.PLUS)
                            {
                                System.out.println("adding "+nested.peek());

                                unrolled.add(nested.pop());
                                continue;
                            }

                            break;
                        }

                        nested.push(Tokens.PLUS);

                        System.out.println("nested now "+nested);
                    }
                    break;
                    case Tokens.MULTIPLY:
                    {
                        System.out.println("nested "+nested);
                        System.out.println("unrolled "+unrolled);

                        System.out.println("character "+Tokens.MULTIPLY);

                        while (!nested.isEmpty() && nested.peek() != Tokens.OPEN_BRACE)
                        {
                            System.out.println("peeked "+nested.peek());
          
                            if ((nested.peek() == Tokens.PLUS) || (nested.peek() == Tokens.MULTIPLY))
                            {
                                System.out.println("adding "+nested.peek());

                                unrolled.add(nested.pop());
                                continue;
                            }

                            break;
                        }

                        nested.push(Tokens.MULTIPLY);

                        System.out.println("nested now "+nested);
                    }
                    break;
                    default:
                    {
                        unrolled.add(lineArray[j]);
                    }
                    break;
                }
            }

            while (!nested.empty())
            {
                unrolled.add(nested.pop());
            }

            System.out.println("got "+unrolled);

            Stack<Long> values = new Stack<Long>();
      
            for (int k = 0; k < unrolled.size(); k++)
            {
                Character c = unrolled.elementAt(k);

                switch (c)
                {
                    case Tokens.PLUS:
                    {
                        values.push(values.pop() + values.pop());
                    }
                    break;
                    case Tokens.MULTIPLY:
                    {
                        values.push(values.pop() * values.pop());
                    }
                    break;
                    default:
                    {
                        values.push((long) Character.getNumericValue(c));
                    }
                    break;
                }
            }
      
            finalResult += values.pop();
        }

        return finalResult;
    }

    private boolean _debug;
}
@@ -1642,47 +1642,8 @@
    %7D
-%0A%0A                        nested.pop();
 %0A   
@@ -1740,32 +1740,98 @@
               %7B%0A
+                        if (_debug)%0A                        %7B%0A    
                 
@@ -1868,32 +1868,36 @@
 sted %22+nested);%0A
+    
                 
@@ -1938,33 +1938,36 @@
 ed %22+unrolled);%0A
-%0A
+    
                 
@@ -2011,32 +2011,58 @@
  %22+Tokens.PLUS);
+%0A                        %7D
 %0A%0A              
@@ -2165,92 +2165,8 @@
   %7B%0A
-                            System.out.println(%22peeked %22+nested.peek());%0A          %0A
     
@@ -2257,86 +2257,8 @@
   %7B%0A
-                                System.out.println(%22adding %22+nested.peek());%0A%0A
     
@@ -2501,75 +2501,8 @@
 US);
-%0A%0A                        System.out.println(%22nested now %22+nested);
 %0A   
@@ -2603,32 +2603,98 @@
               %7B%0A
+                        if (_debug)%0A                        %7B%0A    
                 
@@ -2731,32 +2731,36 @@
 sted %22+nested);%0A
+    
                 
@@ -2801,33 +2801,36 @@
 ed %22+unrolled);%0A
-%0A
+    
                 
@@ -2878,32 +2878,58 @@
 okens.MULTIPLY);
+%0A                        %7D
 %0A%0A              
@@ -3032,92 +3032,8 @@
   %7B%0A
-                            System.out.println(%22peeked %22+nested.peek());%0A          %0A
     
@@ -3164,86 +3164,8 @@
   %7B%0A
-                                System.out.println(%22adding %22+nested.peek());%0A%0A
     
@@ -3412,75 +3412,8 @@
 LY);
-%0A%0A                        System.out.println(%22nested now %22+nested);
 %0A   
@@ -3744,32 +3744,60 @@
 %0A            %7D%0A%0A
+            if (_debug)%0A    
             Syst
@@ -3816,11 +3816,21 @@
 ln(%22
-got
+unrolled now:
  %22+u

Update MathsParser.java