Results 1 to 12 of 12
Thread: Breaking up a Chemical Formula
- 12-21-2011, 09:26 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Breaking up a Chemical Formula
I'm fairly new to programming, and I'm currently working on a program that takes a chemical formula consisting of carbon, nitrogen, oxygen, and hydrogen (ex. C2H3O2), and calculates their molar mass. The primary question I'm struggling with is this: How do I divide the formula into its constituent elements and corresponding subscripts, each in separate arrays (this must accomodate for elements with no subscript, like CH4, as well as for subscripts with multi-digit subscripts, like C6H12O6)?
Thank you, for whatever information with which you may be able to provide regarding this task.
- 12-21-2011, 10:07 PM #2
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Breaking up a Chemical Formula
Use a regular expression to do pattern matching
Lesson: Regular Expressions (The Java Tutorials > Essential Classes)
- 12-26-2011, 05:52 AM #3
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Re: Breaking up a Chemical Formula
I've been reading that article and looking at resources on regex, but I'm not completely sure how to actually implement a regular. If you could give me an outline of the sort of syntax (or semantics, if that's what you're into) I would use to break up a formula, I think I would be able to get a much better sense of how a regex works, and where it would be practically implemented.
- 12-26-2011, 11:56 AM #4
Member
- Join Date
- Dec 2011
- Posts
- 2
- Rep Power
- 0
Re: Breaking up a Chemical Formula
sorry im learning a b c of java i cant help that much.
i did major in chemistry but im sure you did too.
- 12-26-2011, 12:07 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,601
- Blog Entries
- 7
- Rep Power
- 17
Re: Breaking up a Chemical Formula
An element name (abbreviation) is a capital letter followed by zero or more lower case letters. Following this element are zero or more digits. This can be expressed as a regular expression as follows:
I added two groups: one for the element name and one for the optional digits. I hope you can take it from here. Read the API documentation for the Pattern class (that's Java's representation of a regular expression) and the Matcher class (a class that matches a String against a regular expression).Java Code:([A-Z][a-z]*)(\\d*)
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-26-2011, 07:17 PM #6
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Re: Breaking up a Chemical Formula
I think I have a pretty complete understanding of how to format a regular expression from reading the API documentation. Right now, I'm struggling to understand how I would actually use these regular expressions to break apart a formula and store its components in an array (if I scan a string for ([CHNO?][\\d?][\\d?), how can I store each character C, H, N, or O and each digit [\\d?][\\d?] in an array?) ? Also, is the only way to scan for a multi-digit number without using two 'zero or one' digits?
- 12-27-2011, 06:35 PM #7
Re: Breaking up a Chemical Formula
I wrote an app that might help. Here is a link:
http://dl.dropbox.com/u/39235514/RegexViewer.jar
and the source for it
http://dl.dropbox.com/u/39235514/RegexViewer.zip
If you can't figure out the app, lemme know. Paste you input into the box, and then start typing your regex. Don't forget that when hard coding the regex into actual java code, you need to double escape -- so, \w becomes \\w
- 12-27-2011, 07:03 PM #8
Moderator
- Join Date
- Jul 2010
- Location
- California
- Posts
- 1,619
- Rep Power
- 5
Re: Breaking up a Chemical Formula
A descent workflow would be to loop over each match, capturing the appropriate groups (letter/number - this needs to be specified in the regular expression using parenthesis), then store these in a Map. You can then iterate over the keys of the map to get the number of atoms, or use the map directly to query how many carbons, oxygens, etc...are present.
-
Re: Breaking up a Chemical Formula
I think that you should actually define all of the known elements first, maybe using an Enum set
Then you could feed a char (C, H, N or O) with an integer into the method Element.getMass('C',4) to return one part of the calculation.Java Code:public enum Element { CARBON ("C", 12.0107), HYDROGEN ("H", 1.00794), NITROGEN ("N", 14.0067), OXYGEN ("O", 15.9994); private final char name; private final double mass; Elements(char name, double mass) { this.name = name; this.mass = mass; } // then you could also have your calculation methods in here public static double getMass(char name, int number) { for (Element e : Element.values() ) if e.name.equals(name) return e.mass*number; return -1; } }
- 01-03-2012, 03:23 AM #10
Member
- Join Date
- Dec 2011
- Posts
- 4
- Rep Power
- 0
Re: Breaking up a Chemical Formula
I've taken into account a lot of the suggestions given here, and I'm in a further stage in the program. Now, I just need some quick advice on understanding how I should implement this regex:
Obviously, it's not completed, but I'm having trouble understanding the error message "illegal character: /92" with a highlight of the codeJava Code:import java.util.Scanner; import java.util.regex.Pattern; import java.util.regex.Matcher; public class molarMass { private final double HYDROGEN = 1.00794; private final double CARBON = 12.0107; private final double NITROGEN = 14.0067; private final double OXYGEN = 15.9994; private static String formula; public molarMass() { getFormula(); createArray(); breakApart(); } public void getFormula() { Scanner in = new Scanner (System.in); System.out.println("Enter your chemical equation consisting of H, C, N, and O: "); formula = in.next(); } public void createArray() { } public void breakApart() { int length = formula.length; for(int i = 0; i>length; i++) { if(substring.formula(i,i+1)==(C|H|N|O)) { } else if(substring.formula(i,i+1)==(\\d)) { } else { } } } }What am I doing wrong?Java Code:else if(substring.formula(i,i+1)==(\\d))
-
Re: Breaking up a Chemical Formula
This makes no sense:
and isn't how regular expressions work. Have you gone through the regular expressions tutorial?Java Code:else if(substring.formula(i,i+1)==(\\d))
Here are the two that I've studied:
For basics: Lesson: Regular Expressions
For advanced: Regular-Expressions.info Tutorial
Regular expressions will take a different way of thinking and can't be guessed at. Please check out the tutorials -- you won't be sorry you did.Last edited by Fubarable; 01-03-2012 at 03:41 AM.
-
Similar Threads
-
Breaking the thread.
By JDaniel in forum Threads and SynchronizationReplies: 4Last Post: 12-21-2010, 03:22 PM -
breaking up
By smray7 in forum New To JavaReplies: 2Last Post: 10-31-2010, 07:58 AM -
Formula Builder
By rbs100 in forum Advanced JavaReplies: 1Last Post: 07-03-2009, 06:57 PM -
Breaking down an integer
By Emily in forum New To JavaReplies: 1Last Post: 03-06-2008, 06:39 PM -
What is the formula?
By yuchuang in forum New To JavaReplies: 3Last Post: 04-30-2007, 10:00 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks