I'm doing homework for my data structures class and ran into a snag at a certain point. Basically, I have an expression like
(varx + vary*varz[(vara+varb[(a+b)*33])])/55
and I have to store the variables in the equation in either of two arraylists, scalar (for simple variables) or arrays (for variables with arrays). In this example, variables like varx or vary would go into the scalar array and varz would be an array because it has a bracket following it right after in the expression. I know StringTokenizer
makes the scalar variables easy to track when I use the code..
StringTokenizer st = new StringTokenizer(expr, " \t*+-/()[]");
However, this doesn't really work for being able to tell what the arrays variable is since it also erases the brackets since the answer that would return if I were to print it is
while(st.hasMoreTokens()){
System.out.println(st.nextToken());
}
and it would print
varx
vary
varz
vara
varb
a
b
33
55
How would I be able to check for a bracket after each variable to tell if it should go in the scalar arraylist
or the arrays arraylist? Thanks for the help!