5 Answers
5

String initialized incorrectly. Double quotes missing at the ends. Remove bracket and brace from the ends.

String myString = "a+b-c*d/e";

3rd problem: -

String array initialized with an String object, rather than an array object.

String[] result=new String(); // Should be `new String[size]`

In fact, you don't need to initialize your array before hand.

4th problem: -

String.split takes a Regular Expression as argument, you have passed an array. Will not work.

Use: -

String[] result = myString.split("[-+*/]");

to split on all the operators.

And regarding your this statement: -

as well as =, -, *, d, / (also an array of operators) separately.

I don't understand what you want here. Your sample string does not contains =. And d is not an operator. Please see if you want to edit it.

UPDATE : -

If you mean to keep the operators as well in your array, you can use this regex: -

String myString= "a+b-c*d/e";
String[] result = myString.split("(?<=[-+*/])|(?=[-+*/])");
System.out.println(Arrays.toString(result));
/*** Just to see, what the two parts in the above regex print separately ***/
System.out.println(Arrays.toString(myString.split("(?<=[-+*/])")));
System.out.println(Arrays.toString(myString.split("(?=[-+*/])")));

As far as I know you cannot do what you are after right out of the box. The split(String regex) does not take an entire array (unlike C#), just a string representing a valid regular expression.

What you could do would be to define a Set which contains your operators and 2 ArrayLists. Once you have that, you iterate over your string, check if the set contains that given character (thus determining if it is an operator or not). If it is, then, you put it in one list, if not, you put it in the other.

I think what you want to do, is more like a parser, rather than a tokenizer.

With a string tokenizer, you usually have a long string (i.e. "parameter1;parameter2;parameter3") with several elements concatenated, and using a "token" to separate these elements. With function "String.split", you are basically saying: "Ok, give all elements from this string, but taking into account that character ';' separates different elements". And then you get "parameter1", "parameter2", "parameter3". You do not care about separators.

But in a mathematical expression like yours: "a+b-c*d/e", you want to get all the individual elements (operands and operators). Here there are no explicit separators, and the order is also important for you. I would use a parser library and write a small grammar instead.