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
2
  • How do you add the 12? userInput.add("12"); or userInput.add("1"); userInput.add("2"); ? Commented Jan 3, 2016 at 14:40
  • userInput.add("12"); @guy Commented Jan 3, 2016 at 14:41

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

my problem is not related to ArrayList variable, it is about string variable in fooConstructor. After parsing string, I can construct then compare.
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)
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
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.