Results 1 to 12 of 12
Like Tree1Likes
  • 1 Post By doWhile

Thread: Breaking up a Chemical Formula

  1. #1
    zeiss is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

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

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Breaking up a Chemical Formula

    Use a regular expression to do pattern matching

    Lesson: Regular Expressions (The Java™ Tutorials > Essential Classes)
    quad64bit likes this.

  3. #3
    zeiss is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

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

  4. #4
    ibwaheemi is offline Member
    Join Date
    Dec 2011
    Posts
    2
    Rep Power
    0

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

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,508
    Blog Entries
    7
    Rep Power
    20

    Default 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:

    Java Code:
    ([A-Z][a-z]*)(\\d*)
    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).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    zeiss is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default 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?

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default 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

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: Breaking up a Chemical Formula

    Quote Originally Posted by zeiss View Post
    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?
    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.

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Breaking up a Chemical Formula

    I think that you should actually define all of the known elements first, maybe using an Enum set

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

  10. #10
    zeiss is offline Member
    Join Date
    Dec 2011
    Posts
    4
    Rep Power
    0

    Default 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:
    Java 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
                {
                }
            }
        }
    }
    Obviously, it's not completed, but I'm having trouble understanding the error message "illegal character: /92" with a highlight of the code
    Java Code:
    else if(substring.formula(i,i+1)==(\\d))
    What am I doing wrong?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Breaking up a Chemical Formula

    This makes no sense:

    Java Code:
    else if(substring.formula(i,i+1)==(\\d))
    and isn't how regular expressions work. Have you gone through the regular expressions tutorial?

    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.

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Breaking up a Chemical Formula

    Also, you will want to look up the String API to see how to call the substring method. For one thing the method must be called on a String object. It can't be called on its own as you are trying to do.

Similar Threads

  1. Breaking the thread.
    By JDaniel in forum Threads and Synchronization
    Replies: 4
    Last Post: 12-21-2010, 03:22 PM
  2. breaking up
    By smray7 in forum New To Java
    Replies: 2
    Last Post: 10-31-2010, 07:58 AM
  3. Formula Builder
    By rbs100 in forum Advanced Java
    Replies: 1
    Last Post: 07-03-2009, 06:57 PM
  4. Breaking down an integer
    By Emily in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 06:39 PM
  5. What is the formula?
    By yuchuang in forum New To Java
    Replies: 3
    Last Post: 04-30-2007, 10:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •