Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



Extract the elements of string in java

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 13-01-2010
Member
 
Join Date: Dec 2009
Posts: 204
Extract the elements of string in java

Hello,
I have a string long enough my aim is to extract from string the elements that constitute it, but it is a little more special.
Quote:
(Other receivables + Accounts receivable)/5.25* ARRESTED IN NV + P Preliminary
the desired result after insertion into the vector cad vector content:
will be as follows
Quote:
Vector v =[(, Other debtors + Accounts receivable,),/,5.25, *, STANDING IN NV, +, preliminary expenses]
It has a function to do this treatment.

Last edited by Ash maker : 13-01-2010 at 01:52 PM.
Reply With Quote
  #2  
Old 13-01-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
Re: Extract the elements of string in java

Hi,
The best is probably construct a parser for it, I advise you to use
Antlr (nothing to do with Ant). Basically you give him your "grammar": a variable is an alphanumeric string, an addition is: Variable '+' variable. Hope this will help you. If you have any more problems then do post back.
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #3  
Old 13-01-2010
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
Re: Extract the elements of string in java

Hello,
Even simpler, with RegExp and method String.split. You can try the following line of code. This will make your code simpler.
Code:
String[] res = s.split("\\+|\\*|/");
I think this is the correct choice for your situation.
Reply With Quote
  #4  
Old 13-01-2010
Member
 
Join Date: Dec 2009
Posts: 204
Re: Extract the elements of string in java

Hello
I tried with splits, though I am not sure that this works correctly and is this the correct way to do it?
Quote:
String s ="(Other receivables + Accounts receivable) / 5.25 * ARRESTED IN NV + Shipping preliminary";
String[] res = s.split("\\+|\\*|/");
System.out.print(res);
the result is the following:
Quote:
[(, Other Receivables, Accounts receivable,),5.25, STANDING IN NV, preliminary expenses]
It Does not operators +,*,/ what is obvious, but my objective is to insert the same operators +,*,/ but respecting their orders in the chain.
Reply With Quote
  #5  
Old 13-01-2010
kelfro's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,976
Re: Extract the elements of string in java

Hello,
The solution is to take the problem in reverse: instead of cutting the string according to certain criteria, it would be wise to find items in the chain.
I understand that I see 3 things to look for
1) Mathematical symbols: (+*/-)
2) Figures
3) The strings (short, everything except the above).
What I translate into this:
Code:
	Final String symbols = "[+-/*()]"; / / A character from the following: + - * / ()
	Final String numbers = "\\d + (\\.\\d *)? "; / / Numbers: at least one number, possibly followed by '. " and optional data
	Final String string = "[^+-/*()\\d] + "; / / A string: All who are no numbers or symbols
Then just look for these items:
Code:
	String input = "(Other receivables + Accounts receivable) / 5.25 * ARRESTED IN NV + Shipping preliminary";

Pattern pattern = Pattern.compiles("("+ + symbols")|("numbers + +")|("+ + strings")");
Matcher matcher = pattern.matcher(input);
	while (matcher.find()) {
		System.out.System.out.println("Found: [" + Matcher.group() + "]");
	}
Now if your need is more complex it will probably define a grammar and use a parser generator such as Javac.
Reply With Quote
  #6  
Old 13-01-2010
Member
 
Join Date: Dec 2009
Posts: 204
Re: Extract the elements of string in java

Hello,
I implemented you code and here is the result
Code:
Final String symbols = "[+-/*()]"; / / A character from the following: + - * / ()
	Final String numbers = "\\d + (\\.\\d *)? "; / / Numbers: at least one number, possibly followed by '. " and optional data
	Final String string = "[^+-/*()\\d] + "; / / A string: All who are no numbers or symbols
String input = "(Other receivables + Accounts receivable) / 5.25 * ARRESTED IN NV + Shipping preliminary";

Pattern pattern = Pattern.compiles("("+ + symbols")|("numbers + +")|("+ + strings")");
Matcher matcher = pattern.matcher(input);
	while (matcher.find()) {
		System.out.System.out.println("Found: [" + Matcher.group() + "]");
	}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Extract the elements of string in java"
Thread Thread Starter Forum Replies Last Post
Sort the elements of a table in java Adrina_g Software Development 4 06-03-2010 11:59 AM
Extract the integer from a string New ID Software Development 5 26-02-2010 12:31 AM
Java program to reverse the order of LinkedList elements? MKAIF Software Development 5 22-01-2010 09:44 PM
How to sort LinkedList elements using java program? KADRI Software Development 4 22-01-2010 08:45 PM
Extract Java File From Sony Ericsson K850 Level8 Portable Devices 5 21-12-2009 02:47 PM


All times are GMT +5.5. The time now is 04:00 AM.