Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

parse a string

 
Amod Gupta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry

 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ugh; just parse it.
 
Amod Gupta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


hw do i parse? @
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Amod Gupta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@henry
what is find() method, i have never heard and i am not able to find it.
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try this:
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@henry please give the example.i want to try.
 
Amod Gupta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Ireneusz Kordal
but i also want the symbols and the order of symbols and numbers must be maintained,
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amod Gupta wrote:@henry please give the example.i want to try.


Okay, I am in a good mood. If another moderator feels that I crossed the line by handing out an answer, please delete this solution...



Henry
 
Ireneusz Kordal
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amod Gupta wrote:@Ireneusz Kordal
but i also want the symbols and the order of symbols and numbers must be maintained,

What exactly does it mean 'order of symbols' ? Do you mean order of operations (*,/ is higher than +,- etc) ?
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ireneusz Kordal wrote:try this:


Ireneusz,

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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

@please donot reply if you do not want to.
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about using Scanner? A small example:
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:How about using Scanner? A small example:


Simple

 
Amod Gupta
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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 Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amod Gupta wrote:
@henry thanks but i cant understand.


Which I kinda mentioned. Regex is not really a subject that can be learned well, by example.

Amod Gupta wrote:
@sequence means i need to know the exact symbols between the numbers after parsing.


Which the solution that Ireneusz provided does. What was wrong with it?

Henry
 
Henry Wong
author
Marshal
Pie
Posts: 21114
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amod Gupta wrote:@rob prime.I am using the following code but getting into an infnite loop any idea?


What happens when the code encounters something that is not a number, and not a operator... such as a parenthesis?

There is a reason that Rob has an extra "else" condition, which he left unimplemented.

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20527
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic