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(lineArray[j]);
                    }
                    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;
                        }
                    }
                    break;
                    case Tokens.PLUS:
                    {
                        boolean done = false;

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

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

                            System.out.println("popped "+c);
                            
                            if (c == Tokens.OPEN_BRACE)
                                done = true;
                            else
                            {
                                if (c == Tokens.PLUS)
                                {
                                    System.out.println("adding "+c);

                                    unrolled.add(c);
                                }
                            }
                        }

                        nested.push(lineArray[j]);
                    }
                    break;
                    case Tokens.MULTIPLY:
                    {
                        boolean done = false;

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

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

                            System.out.println("popped "+c);

                            if (c == Tokens.OPEN_BRACE)
                                done = true;
                            else
                            {
                                if ((c == Tokens.PLUS) || (c == Tokens.MULTIPLY))
                                {
                                    System.out.println("adding "+c);

                                    unrolled.add(c);
                                }
                            }
                        }

                        System.out.println("pushing "+lineArray[j]);

                        nested.push(lineArray[j]);
                    }
                    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;
}
@@ -1735,32 +1735,161 @@
               %7B%0A
+                        System.out.println(%22nested %22+nested);%0A                        System.out.println(%22unrolled %22+unrolled);%0A%0A
                 
@@ -1910,32 +1910,32 @@
 n done = false;%0A
-
 %0A               
@@ -2197,37 +2197,9 @@
 c);%0A
-                            
 %0A
+
     
@@ -2816,32 +2816,185 @@
               %7B%0A
+                        System.out.println(%22nested %22+nested);%0A                        System.out.println(%22unrolled %22+unrolled);%0A                        %0A
                 
@@ -3762,32 +3762,32 @@
               %7D%0A
+
                 
@@ -3801,78 +3801,8 @@
  %7D%0A%0A
-                        System.out.println(%22pushing %22+lineArray%5Bj%5D);%0A%0A
     

Update MathsParser.java