aspose file tools*
The moose likes Beginning Java and the fly likes parse a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "parse a string" Watch "parse a string" New topic
Author

parse a string

Amod Gupta
Greenhorn

Joined: Oct 16, 2009
Posts: 28
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
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Ugh; just parse it.
Amod Gupta
Greenhorn

Joined: Oct 16, 2009
Posts: 28
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

Joined: Oct 02, 2003
Posts: 11312
    
  16

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...

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Amod Gupta
Greenhorn

Joined: Oct 16, 2009
Posts: 28
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
@henry
what is find() method, i have never heard and i am not able to find it.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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

Joined: Jun 21, 2008
Posts: 423
try this:
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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
@henry please give the example.i want to try.
Amod Gupta
Greenhorn

Joined: Oct 16, 2009
Posts: 28
@Ireneusz Kordal
but i also want the symbols and the order of symbols and numbers must be maintained,
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

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
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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

Joined: Jun 21, 2008
Posts: 423
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
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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

Joined: Oct 16, 2009
Posts: 28
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

Joined: Oct 27, 2005
Posts: 19693
    
  20

How about using Scanner? A small example:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Rob Prime wrote:How about using Scanner? A small example:


Simple

Amod Gupta
Greenhorn

Joined: Oct 16, 2009
Posts: 28
@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
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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
Sheriff

Joined: Sep 28, 2004
Posts: 18834
    
  40

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

Joined: Oct 27, 2005
Posts: 19693
    
  20

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://aspose.com/file-tools
 
subject: parse a string