Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Okay so after all the great advise from the guys I created a method that will take an input such as <code>-1+2(4+(2*(-2--1)))*-1.5</code> and split it to an array, such as <code>[-1, +, 2, (, 4, +, (, 2, *, (, -2, -, -1, ), ), ), *, -1.5]</code>.</p> <p>The way the method works is that it splits the input String using regex. With regex I was able to split all the numbers and operators. While this is great it wasn't able to handle negative values. Using regex it would always see <code>-</code> as a binary operator. I needed it to see it as a unary operator so that it could understand that it's a negative value. So what I did was compare each operator with the string that followed it. If this was also an operator I knew that the second one was a unary operator. I then also had to put in an if statement for if the first value was a <code>-</code> and if it was I knew that that was a unary operator.</p> <p>Here's the code so far. I'm sure there is an easier way to do this using a parser, I just couldn't wrap my head around it.</p> <pre><code>import java.util.ArrayList; import java.util.Arrays; public class expSplit { public String[] splitExp(String theexp){ ArrayList&lt;String&gt; tokens = new ArrayList&lt;String&gt;(); //System.out.println(theexp); String expression = theexp.replaceAll("\\s+", ""); //System.out.println(expression); String tem[] = expression.split("(?&lt;=[-+*/%(),])(?=.)|(?&lt;=.)(?=[-+*/%(),])"); ArrayList&lt;String&gt; temp = new ArrayList&lt;String&gt;(Arrays.asList(tem)); String orig = null; String regex = "[-+/%*]+"; String first = temp.get(0); tokens.add(first); String secound = temp.get(1); if(first.equals("-")){ tokens.remove(0); tokens.add(first+secound); } for (int i = 0; i &lt; temp.size(); i++) { String a = temp.get(i); if (i &gt;= 1){ String b = temp.get(i-1); if(b.matches(regex) &amp;&amp; a.matches("[-+]+")){ String c = temp.get(i-2); if(c.matches("[-+]+")){ //System.out.println("MATCH"); break; }else{ //System.out.println("NO MATCH"); orig = a; } }else if(orig != null &amp;&amp; orig.equals("-")){ tokens.add(orig + a); orig = null; }else{ tokens.add(a); } } } if(first.equals("+")){ tokens.remove(0); } if(first.equals("-")){ tokens.remove(1); } String[]tokenArray = new String[tokens.size()]; tokenArray = tokens.toArray(tokenArray); //System.out.print(tokens); return tokenArray; } } </code></pre> <p>Thanks for the help, Ciaran</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload