2

I try to write equals override function. I think I have written right but the problem is that parsing the expression. I have an array type of ArrayList<String> it takes inputs from keyboard than evaluate the result. I could compare with another ArrayList<String> variable but how can I compare the ArrayList<String> to String. For example,

String expr = "(5 + 3) * 12 / 3";
ArrayList<String> userInput = new ArrayList<>();
userInput.add("(");
userInput.add("5");
userInput.add(" ");
userInput.add("+");
userInput.add(" ");
userInput.add("3");
.
.
userInput.add("3");
userInput.add(")");

then convert userInput to String then compare using equals As you see it is too long when a test is wanted to apply. I have used to split but It splits combined numbers as well. like 12 to 1 and 2

public fooConstructor(String str) 
{
   // ArrayList<String> holdAllInputs; it is private member in class
   holdAllInputs = new ArrayList<>();

    String arr[] = str.split("");

    for (String s : arr) {
        holdAllInputs.add(s);
    }
}

As you expect it doesn't give the right result. How can it be fixed? Or can someone help to writing regular expression to parse it properly as wanted? As output I get:

(,5, ,+, ,3,), ,*, ,1,2, ,/, ,3 

instead of

(,5, ,+, ,3,), ,*, ,12, ,/, ,3
Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
askque
  • 319
  • 2
  • 9

3 Answers3

3

The Regular Expression which helps you here is

"(?<=[-+*/()])|(?=[-+*/()])"

and of course, you need to avoid unwanted spaces.

Here we go,

String expr = "(5 + 3) * 12 / 3";
.
. // Your inputs
.
String arr[] = expr.replaceAll("\\s+", "").split("(?<=[-+*/()])|(?=[-+*/()])");
for (String s : arr) 
{
    System.out.println("Element : " + s);
} 

Please see my expiriment : http://rextester.com/YOEQ4863

Hope it helps.

Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43
0
this makes all the inputs into one string which can then be can be compared against the expression to see if it is equal

String x = "";    

for(int i = 0; i < holdAllInputs.length; i++){
    x = x + holdAllInputs.get(i);
}

if(expr == x){
   //do something equal
}else{
   //do something if not equal
}
Sean Powell
  • 564
  • 4
  • 20
0

Instead of splitting the input into tokens for which you don't have a regex, it would be good to move ahead with joining the strings in the List like:

StringBuilder sb = new StringBuilder();
for (String s : userInput)
{
    sb.append(s);
}

then use sb.toString() later for comparison. I would not advice String concatenation using + operator details here.

Another approach to this would be to use one of the the StringUtils.join methods in Apache Commons Lang.

import org.apache.commons.lang3.StringUtils;

String result = StringUtils.join(list, "");

If you are fortunate enough to be using Java 8, then it's even easier...just use String.join

String result = String.join("", list);

More details on this approach available here

Community
  • 1
  • 1
  • my problem is not related to ArrayList variable, it is about string variable in fooConstructor. After parsing string, I can construct then compare. – askque Jan 03 '16 at 14:59
  • That's what I wanted to say. Do not parse the String variable in `fooConstructor`. Instead combine the Strings in List to perform comparison. (Unless you want to use some regex which is hard to digest) – Aaditya Gavandalkar Jan 03 '16 at 15:01