data structure and algorithms
package array; public class Tokenizer { private String str; int curIndex; public Tokenizer(String str) { this.str = str; curIndex = 0; } boolean hasNext() { return curIndex != str.length(); } public String next() { int i = indexOf(curIndex); if( i == curIndex) { curIndex = i+1; return str.substring(i, i+1); } String tmp = str.substring(curIndex, i); curIndex = i; return tmp; } //get the index of +, -, *, /, (, ) starting from start private int indexOf(int start) { int i = start; char c = str.charAt(i); while( c != '+' && c != '-' && c != '*' && c != '/' && c != '(' && c != ')') { i++; if ( i == str.length()) { return i; } c = str.charAt(i); } return i; } public void reset() { curIndex = 0; } public static void main(String[] arg) { Tokenizer t = new Tokenizer("a+b+(3.3-3*0.555555)"); while(t.hasNext()) { System.out.println(t.next()); } //t.reset(); while(t.hasNext()) { System.out.println(t.next()); } } }