This week's giveaways are in the MongoDB and Jobs Discussion forums. We're giving away four copies of Mongo DB Applied Patterns and 4 resume reviews from Five Year Itch and have the authors/reps on-line! See this thread and this one for details.
How can i parse an expression of the type(8+9*7/9) which i input as a string.I want every number as well as symbol seperated.This cannot be done directly by using split() function as for using it i will have to reformat the expression as(8 + 9 * 7 / 9) which is not possible to do.I was using split(" ").Please help.Thanks
Amod Gupta wrote:This cannot be done directly by using split() function as for ...
Actually, it can. It is just not a very "pretty" regex. It can be done with an OR of either a look-ahead for an operator, or a look-behind for an operator. Regardless, it is probably better to use the find() method instead, as you don't really have a good definition of a delimiter here.
i cannot understand can you please give an example.I am basically trying to build an expression calculator and i want all the operators as well as numbers to be stored as seperate strings.@Henry
brute force would read the string character by character. you have a small number of token options, and the grammar is pretty well defined... For example, you can't have a / followed by a +. There are no two-character tokens that are not numbers.
I'd probably try checking each character to see if it's a digit. if so, it's part of the current token, so add it on, then look at the next.
When you hit a non-digit, you know you're last token is done, and you have your current one. Then start building the next.
so...
8+9*7/9
8 is a digit
+ is not, so you now have a tokens of "8" and "+".
9 is a digit
* is not, so you now further have "9" and *
etc...
Never ascribe to malice that which can be adequately explained by stupidity.
Amod Gupta
Greenhorn
Joined: Oct 16, 2009
Posts: 28
posted
0
and how do we actually bryteforce in java this is not as simple as in c.Also it is possible to have any alphabet(a to z) in the string.
Amod Gupta
Greenhorn
Joined: Oct 16, 2009
Posts: 28
posted
0
@henry
what is find() method, i have never heard and i am not able to find it.
Amod Gupta wrote:i cannot understand can you please give an example.I am basically trying to build an expression calculator and i want all the operators as well as numbers to be stored as seperate strings.@Henry
Regular expressions is not really something that can be learnt by example. If you really want this solution (split() using regex), I can give it to you, but unless you understand regex, you won't understand how it works.
Amod Gupta wrote:@henry
what is find() method, i have never heard and i am not able to find it.
The split() method of the String class is actually a convenience method. It makes a call to the regex engine for you. I am suggesting to use the regex engine directly via the java.util.regex.Pattern class.
Henry
Amod Gupta
Greenhorn
Joined: Oct 16, 2009
Posts: 28
posted
0
@henry please give the example.i want to try.
Amod Gupta
Greenhorn
Joined: Oct 16, 2009
Posts: 28
posted
0
@Ireneusz Kordal
but i also want the symbols and the order of symbols and numbers must be maintained,
Amod, your last few posts look a lot like "Gimme the code". The Ranch is NotACodeMill; but I hope you didn't really mean it that way. Take Henry's advice, and if you really want to use regex for this problem then start with the examples you have been given and adapt them to your requirements. As Henry suggested, that could take a while. So ask relevant questions as you go along, if you get stuck.
If you put the operators in a character class, then you wouldn't need to escape them. Heck, you wouldn't need the OR ("|") either.
Henry
Amod Gupta
Greenhorn
Joined: Oct 16, 2009
Posts: 28
posted
0
ok.then i can think of a solution but i am not able to implement it.
1st split using +
then the tokens that are not pure numbers are split using -
then split using *
then split using /
now solve from bottom to top.
now this sounds interesting but how to implement it.
@henry thanks but i cant understand.
@ yes i need the code, i am a beginner so i need some help to get into this thing,ok i dont want you to do my work,but you explain it in a way a beginner can understand otherwise there is no need to tell it is not worth wating your time as well as mine.
@sequence means i need to know the exact symbols between the numbers after parsing.
Rob Prime wrote:How about using Scanner? A small example:
Simple
Amod Gupta
Greenhorn
Joined: Oct 16, 2009
Posts: 28
posted
0
@rob prime.I am using the following code but getting into an infnite loop any idea?
Scanner scanner = new Scanner(str);
Pattern pattern = Pattern.compile("[*+/-]");
while (scanner.hasNext())
{
if (scanner.hasNextInt())
{
int number = scanner.nextInt();
arr[t]=""+number;
}
else if (scanner.hasNext(pattern))
{
String operator = scanner.next(pattern);
arr[t]=operator;
}
t++;
}
Henry is right. Whatever happens, you should always consume the next thing. Otherwise it's still there, and hasNext() will simply return true again, and again, and again...
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.