Results 1 to 7 of 7
  1. #1
    Slayer is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default Unknown Variable Types

    Alright, I'm not sure how to explain this clearly, but I'll give it a shot.

    I want to make a program that will tell me the molar mass of molecules. I don't know how separate whether an integer or a variable will come next.

    For instance, I give all my elements values, like
    double c = 12;
    couble h = 1;
    double o = 16;


    I don't know how to make the program know how to evaluate a person inputting H2O as well as doing OH. How can I make it take the number in the middle?

    Idk, I know I'm not too clear with this and I don't have any code to show you because honestly I don't know where to start.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You don't actually know whether a digit or a letter will come next. For a very simple case you could work along a string like "H2SO4" and test each character to see if it is a letter or a digit.

    For more complex cases (integer values represented by multiple digits, lower case letters, parentheses) you are trying to tokenize and parse the string. This is fun, but may be beyond where you are now.

    It might be best to describe the range of molecular expressions you are trying to work with.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    The parser had better be case-sensitive, or sodium will break down into nitrogen and argon ;)

    db

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,015
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Darryl.Burke View Post
    The parser had better be case-sensitive, or sodium will break down into nitrogen and argon ;)
    True ;-) I forgot most of those molecular expressions so here is a very simple question: do those numbers bind to the last atom or to a latest molecular sub-structure? e.g. in H2SO4 does the '4' bind to the right most 'O' or can it also bind to the group 'SO'? (I know here it binds to the single O, but it's just an example). Do parentheses occur in those expressions? If not, the parsing of such expressions is simple: {<atom><number>?}+

    kind regards,

    Jos

  5. #5
    Slayer is offline Member
    Join Date
    Oct 2010
    Posts
    3
    Rep Power
    0

    Default

    You don't actually know whether a digit or a letter will come next. For a very simple case you could work along a string like "H2SO4" and test each character to see if it is a letter or a digit.
    I think they are simple enough to do this. How do I test each character to tell if is a letter or a digit?

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    Get the character at a position, then invoke isDigit or isLetter.

    The API is a wonderful thing!

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. variable number of fields with different types
    By Sir G. in forum Advanced Java
    Replies: 11
    Last Post: 03-29-2010, 12:40 AM
  2. nextInt(unknown source)
    By babyburger in forum New To Java
    Replies: 3
    Last Post: 03-18-2010, 04:03 PM
  3. Dynamic types for a variable.
    By Somelauw in forum New To Java
    Replies: 5
    Last Post: 11-27-2009, 10:38 AM
  4. Unknown design pattern
    By Steve11235 in forum Advanced Java
    Replies: 4
    Last Post: 07-02-2009, 12:40 PM
  5. Unknown problem
    By sanchir0805 in forum New To Java
    Replies: 4
    Last Post: 03-20-2009, 03:56 AM

Posting Permissions

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