Results 1 to 3 of 3
  1. #1
    thekermo is offline Member
    Join Date
    Oct 2008
    Posts
    1
    Rep Power
    0

    Default Reading in strings

    Hey guys, i'm making a program that will read in a simple formula, decompose it into it's subformals, work out the truth values of these and then print out the results. Most of this I can do without any problem but i'm struggling decomposing the damn formulas. A basic example of a formula would be a(b(a,b)). I need to be able to split this down to a, b, (a,b) and b(a,b) and store those four values into an array. The storing i can do fine but I can't get the string to split up accurately. I've played around with stringTokenizer and streamTokenizer but i'm not sure if that's really the way to go. Any ideas? Any advice will be much appreciated and if I could, i'd give you all some cake.

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    7

    Smile recrusive descent parser

    This challenge is traditional computer science, called a recursive descent parser. Suggest to start google for just exacly that term, there is a list of resources at Wikipedia - Recursive descent parser See as well David J. Eck and Sedgewick's "Algorithms in Java".
    Quote Originally Posted by thekermo View Post
    Hey guys, i'm making a program that will read in a simple formula, decompose it into it's subformals, work out the truth values of these and then print out the results.
    Define "subformals" and truth values.
    Quote Originally Posted by thekermo View Post
    Most of this I can do without any problem but i'm struggling decomposing the damn formulas.
    Digraphic Algorithm Mutating Nuance - D.A.M.N. be sure to acronymize your Clear Understandable Student Studies - C.U.S.S. 'in. ( yuk-yuk !)
    Quote Originally Posted by thekermo View Post
    A basic example of a formula would be a(b(a,b)). I need to be able to split this down to a, b, (a,b) and b(a,b) and store those four values into an array.
    Combinatorics. Donald E. Kuth has fascicle 0 for vol-4 in print now, that may be of some utility in basics and is the definitive reference for the work you have to hand.
    Quote Originally Posted by thekermo View Post
    The storing i can do fine but I can't get the string to split up accurately. I've played around with stringTokenizer and streamTokenizer but i'm not sure if that's really the way to go.
    I would skip the stream stuff for basic studies, place the sample data in an Array or List directly in the sample code. eg:
    Java Code:
    class SampleCombinatorics{
    private static final String sampleData = "a(b(a,b))";
    Doesn't exactly do what I would think of as building a Tree but is much more useful during concepting and studies plus is easier to modifiy the sample data. You can go to a traditional io file at any time onces something appears to work.
    Quote Originally Posted by thekermo View Post
    Any ideas? Any advice will be much appreciated and if I could, i'd give you all some cake.
    I prefer Chocolate, heavy on the Chocolate and premium grade. Check shipping regulations and throw in one standard grade apple pie to cover a bet I have with a master, I have the flag already covered.

    Whatever you do, don't send me any low-carb anything. Oh, and yes - Cay S. Horstmann has some remarkably clear writings on the subject. No shortage of study material - focus on Lists, Trees, Stacks and related matters - reading in the data is a distraction at this point.

    I do not see any tokens in your sample data, why are you using StringTokenizer?
    Java Code:
    String[] result = "this is a test".split("\\s");
    for (int x=0; x<result.length; x++)
    System.out.println(result[x]);
    "a(b(a,b))" - there's nothing to split on. Try if(nextCharacter = "(")StackPush(); or something - the way this is done has factors in common with the size of the dataset and efficiency.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,562
    Rep Power
    23

    Default

    I can't get the string to split up accurately
    Can you show us the code you are using to parse the expressions?

Similar Threads

  1. Subtracting Strings
    By ravian in forum New To Java
    Replies: 7
    Last Post: 10-08-2009, 06:26 PM
  2. Help with drawing strings!
    By JavaInLove in forum AWT / Swing
    Replies: 1
    Last Post: 02-05-2008, 03:39 AM
  3. Help with Vectors and Strings...
    By kaban in forum New To Java
    Replies: 2
    Last Post: 12-09-2007, 09:04 AM
  4. Comparing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-03-2007, 09:44 AM
  5. reversing Strings
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-11-2007, 08:24 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
  •