   [java] String to LinkedList
  Hi again, Im trying to put an string into a LinkedList in this way:String:  (1-2)*3-(1+2)  List:  [ (,1,-,2,),*,3,-,(,1,+,2,) ]String:  (11-2)*3-(155+22)-2   List:  [ (,11,-,2,),*,3,-,(,155,+,22,),-,2 ]The first one is easy I just:    String formula = "(1-2)*3-(1+2)";    char [] ArrayInfija = formula.toCharArray();   for(int i=0; i<ArrayInfija.length;i++)   {    String s = String.valueOf(ArrayInfija);  lstInfija.addLast(s);   }but I still can't do the second. I tried this:  String formula = "(11-2)*3-(155+22)-2";  char [] ArrayInfija = formula.toCharArray();  for(int i=0; i<ArrayInfija.length;i++)  { System.out.println(lstInfija); int offset = 0; String s = String.valueOf(ArrayInfija); if(!EsDigito(s)) {lstInfija.addLast(s); } else {int j=i+1;String temp = String.valueOf(ArrayInfija[i+1]);while(EsDigito(temp)){    s = s + temp;    temp = String.valueOf(ArrayInfija[i+1]);    i++;}lstInfija.addLast(s);i++; } lstInfija.addLast(s);  }but i doesnt work. Any idea?Thanks :)   
  Problem is almost solve I just have a problem at the end of the string:    public Formula(String formula)    {   try   {  this.sInfija = formula;  char [] ArrayInfija = formula.toCharArray();  System.out.println(ArrayInfija.length);  for(int i=0; i<ArrayInfija.length;i++)  { System.out.println("i= "+i + "  " + lstInfija); int offset = 0; String s = String.valueOf(ArrayInfija); if(!EsDigito(s)) {lstInfija.addLast(s); } else {int j=i;while(EsDigito(String.valueOf(ArrayInfija[j])) && j<ArrayInfija.length){    j++;}for(int a=i;a<j-1;a++){    s += String.valueOf(ArrayInfija[a+1]);}lstInfija.addLast(s);i=j-1; } //lstInfija.addLast(s);  }  System.out.print(lstInfija);   }   catch(Exception ex)   {   }    };  you can do it with this: String formula = "(11-2)*3-(155+22)-2";System.out.println(formula);char[] ArrayInfija = formula.toCharArray();List<String> lstInfija = new ArrayList<String>();for (int i = 0; i < ArrayInfija.length; i++) {String s = String.valueOf(ArrayInfija);if (EsDigito(s)) {if (i + 1 < ArrayInfija.length) {String temp = String.valueOf(ArrayInfija);<br><span class="java-keyword">while</span> (EsDigito(temp) &amp;&amp; i + <span class="java-number">1</span> &lt; ArrayInfija.length) {<br>s = s + temp;<br>i++;<br>temp = String.valueOf(ArrayInfija);<br>}<br>}<br>lstInfija.add(s);<br>} <span class="java-keyword">else</span> {<br>lstInfija.add(s);<br>}<br><br>}<br><br>System.out.println(lstInfija);<br><br></pre></div><!–ENDSCRIPT–><br><br>Buy you should start writing things like this:<br><!–STARTSCRIPT–><!–source lang="java"–><div class="source"><pre><br><span class="java-visibilitymodifier">private</span> <span class="java-primitives">char</span>[] arrayInfija;<br><span class="java-visibilitymodifier">private</span> List&lt;String&gt; lstInfija;<br><br><span class="java-visibilitymodifier">private</span> String getSymbol(<span class="java-primitives">int</span> i) {<br><span class="java-keyword">if</span> (i &lt; arrayInfija.length) {<br><span class="java-keyword">return</span> String.valueOf(arrayInfija<span style="font-weight:bold;">);<br>} <span class="java-keyword">else</span> {<br><span class="java-keyword">return</span> <span class="java-keyword">null</span>;<br>}<br>}<br><span class="java-visibilitymodifier">private</span> <span class="java-primitives">boolean</span> isDigitAtNext(<span class="java-primitives">int</span> i) {<br><span class="java-keyword">return</span> isDigitAt(i + <span class="java-number">1</span>);<br>}<br><span class="java-visibilitymodifier">private</span> <span class="java-primitives">boolean</span> isDigitAt(<span class="java-primitives">int</span> i) {<br><span class="java-keyword">return</span> isDigit(getSymbol(i));<br>}<br><span class="java-visibilitymodifier">private</span> <span class="java-primitives">boolean</span> isDigit(String s) {<br><span class="java-keyword">try</span> {<br>Integer.parseInt(s);<br><span class="java-keyword">return</span> <span class="java-keyword">true</span>;<br>} <span class="java-keyword">catch</span> (NumberFormatException e) {<br><span class="java-keyword">return</span> <span class="java-keyword">false</span>;<br>}<br>}<br><br><span class="java-visibilitymodifier">public</span> List&lt;String&gt; translate(String expression) {<br>System.out.println(expression);<br>arrayInfija = expression.toCharArray();<br>lstInfija = <span class="java-keyword">new</span> ArrayList&lt;String&gt;();<br><br><span class="java-keyword">for</span> (<span class="java-primitives">int</span> i = <span class="java-number">0</span>; i &lt; arrayInfija.length; i++) {<br>System.out.println(lstInfija);<br>String symbol = getSymbol(i);<br><span class="java-keyword">if</span> (isDigit(symbol)) {<br><span class="java-keyword">while</span>(isDigitAtNext(i)) {<br>i++;<br>symbol += getSymbol(i);<br>}<br>lstInfija.add(symbol);<br>} <span class="java-keyword">else</span> {<br>lstInfija.add(symbol);<br>}<br>}<br><span class="java-keyword">return</span> lstInfija;<br>}<br><br></pre></div><!–ENDSCRIPT–><br>this is still pretty far away of being a decent code though.<br><br>Why are you using a LinkedList?, why not just a List?<br>Why don't you follow <a href="http://java.sun.com/docs/codeconv/">standard Java Code Conventions</a>?;  If you're not especially concerned with efficiency, you can just use StringTokenizer.static List<String> parse(String f) {List<String> L = new LinkedList<String>();StringTokenizer st = new StringTokenizer(f, "+-*&#47;()", true);while (st.hasMoreTokens())L.add(st.nextToken());return L;};  StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split  method of String or the java.util.regex package instead.   ;  Yeah, well...import java.util.*;import java.util.regex.*;public class Parser {static List<String> parse(String f) {Pattern p = Pattern.compile("[+\\-*&#47;()]");Matcher m = p.matcher(f);List<String> L = new LinkedList<String>();int e = 0;while (m.find()) {offer(L, f.substring(e, m.start()));offer(L, m.group());e = m.end();}offer(L, f.substring(e, f.length()));return L;}static void offer(List<String> L, String s) {if (!s.isEmpty())L.add(s);}public static void main(String[] args) {System.out.println(parse("(11-2)*3-(155+22)-2"));}}Not quite as trivial, is it? I'll stick to using StringTokenizer for what it's good for.   ;  My previous post comes from the StreamTokenizer javadoc.This seems trivial to me (at least as soon as you see the regexp trick [^abc]+|[abc]):import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Tokenizer {private Pattern pattern;public Tokenizer(String symbols) {String regex = "[^" + symbols + "]+|[" + symbols + "]";pattern = Pattern.compile(regex);}List<String> split(String input) {List<String> result = new ArrayList<String>();Matcher matcher = pattern.matcher(input);while(matcher.find()) {result.add(matcher.group());}return result;}public static void main(String[] args) {String symbols = "+\\-*&#47;\\^()";Tokenizer tokenizer = new Tokenizer(symbols);String input = "11+22-33*44&#<span class="java-number">47</span>;(<span class="java-number">55</span>^<span class="java-number">66</span>)"</span>;<br>System.out.println(tokenizer.split(input));<br>}<br>}<br></pre></div><!--ENDSCRIPT--><br>It has the advantage that the regular expression (pattern) can be reused with several input strings.<br>With a little additional work it could be much more powerful, allowing complex symbols like:<br>String symbols = "+\\-*/\\^()[abs][sqrt][sin][cos][tan]";