Here you can find the source of tokens(String exp)
public static ArrayList<String> tokens(String exp)
//package com.java2s; //License from project: LGPL import java.util.ArrayList; public class Main { public static ArrayList<String> tokens(String exp) { ArrayList<String> array = new ArrayList<String>(); String subexp = ""; int numPar = 0; int start = 0; boolean open = false; for (int i = 0; i < exp.length(); i++) { if (exp.charAt(i) == '(') { open = true;/* w w w .ja v a 2s. c om*/ if (numPar == 0) start = i; numPar++; } else if (exp.charAt(i) == ')') { numPar--; } if (numPar == 0 && open) { open = false; subexp = exp.substring(start, i + 1); array.add(subexp); } } return array; } }