Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Parsing String  RSS feed

 
Stribor Kab
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to pass string "12+4" and produce result integer of 16. Can someone just point me in the right direction. I am confused since this is String input.
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The Java core libraries has a built in JS engine. Now, obviously, this engine was designed to run JavaScript... but you can simply wrap the expression string with some JS boilerplate code, and let the engine evaluate the expression for you.

https://docs.oracle.com/javase/8/docs/technotes/guides/scripting/prog_guide/api.html

Henry
 
Stribor Kab
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any other way to parse this?
 
Liutauras Vilda
Marshal
Posts: 3961
214
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stribor Kab wrote:is there any other way to parse this?

You could write your own parser, which parses operands and operators and then builds an expression and evaluates it.
Depending on how complex expressions might be, all that can become quite challening, but doable.
 
Campbell Ritchie
Sheriff
Posts: 54033
130
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:. . . that can become quite challening, but doable.
Parsers are not that difficult to write. Lecturers used to take if for granted that they could teach 2nd year undergraduates how to write a compiler for a basic Turing‑complete language.
In the case of a simple expression like 1+2, you can iterate the String char by char until you find something which cannot be part of a number, here the +. Once the + is passed, the remainder of the text is a number. There are of course many other ways to do it.
Parsing calculators to maintain the precedences of the operators is harder, unfortunately. You push the values onto a Stack until you find an operator, then you pop however many values are required from the stack. The + operator has an arity of 2 (binary operator) so you would pop two values. Something like √ has an arity of 1, so you pop only one value. Unfortunately − can have an arity of 1 or 2 (=sign‑change or subtraction).
 
Stribor Kab
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Liutauras Vilda wrote:In the case of a simple expression like 1+2, you can iterate the String char by char until you find something which cannot be part of a number, here the +. Once the + is passed, the remainder of the text is a number..


Can you do something like that using StringTokenizer but what happens if expression is 2+2+3?
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stribor Kab wrote:
Can you do something like that using StringTokenizer but what happens if expression is 2+2+3?


The StringTokenizer is used to break a string into tokens. In your case, the tokens are the operators and operands.

You have to configure the delimiters of the tokenizer, to correctly break the string into the operators and operands. And you still have to write the code that will evaluate the tokens to the result.

Henry 
 
salvin francis
Bartender
Posts: 1493
23
Eclipse IDE Google Web Toolkit Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As long as you are simply supporting  "12+4" or similar 2 operation based functionality, a simple breaking up of strings should be fine using split with regex.
StringTokenizer is a class which is present for legacy purposes only.

since you have only given examples like 12+4 and 2+2+3, you can split it and simply add them up.
Are you expecting your code to evaluate something like 10+3-5 too ?

If the complexity of the expression is expected to be high, I would lean towards using the javascript engine as henry suggested.
 
Henry Wong
author
Sheriff
Posts: 23026
120
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Okay, this doesn't check the parameters / expression; this doesn't deal with the exceptions; and of course, is susceptible to code injection; ... but ... look how simple the solution is.



Additionally, it doesn't require any third party libraries, and also, will parse every expression mentioned so far (with precedence supported).


So... why won't you use it?

Henry
 
Stribor Kab
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
salvin francis wrote:As long as you are simply supporting  "12+4" or similar 2 operation based functionality, a simple breaking up of strings should be fine using split with regex.
StringTokenizer is a class which is present for legacy purposes only.

since you have only given examples like 12+4 and 2+2+3, you can split it and simply add them up.
Are you expecting your code to evaluate something like 10+3-5 too ?

If the complexity of the expression is expected to be high, I would lean towards using the javascript engine as henry suggested.

Yes this one 10+3-5 is possible too
 
Liutauras Vilda
Marshal
Posts: 3961
214
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's start from requirements then, because now we need to pull words from you, while supposed to be other way round.
Tell everything you know what is needed. Tell if it is homework. Tell if you have any restrictions on what you can use and what you cannot.
 
Stribor Kab
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not homework. I just want to learn how to parse the string by encountering + and -
 
Liutauras Vilda
Marshal
Posts: 3961
214
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stribor Kab wrote:It is not homework. I just want to learn how to parse the string by encountering + and -

Then it is fairly easy as you don't deal with parentheses or multiplications or divisions or other more complicated stuff.

There are multiple ways of doing this, one of those you can simply split string by the space, so you get an array of Strings. You then can convert to char array if you want to, might could be useful.
Handy can be for instance one of static Character class methods isDigit(char c), so you know how to find out if it is a digit or not, if not, then it is an operator.

Then think a little, how you could start. For the draft solution you could probably use two or three variables, switch statement, maybe an 'if' too and do a primitive calculation iterating over the array.

This is what first came to my mind, do some research, most likely there are better ways of doing that.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!