Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Parenthetical input

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Parenthetical input

    Hi, I still sort of a beginner (self taught) and I want to jump into a project that is covers things I'm not used to.

    What I really want to do is deal with logical equation like ~(a&b), but my problem works the same for parenthetical math equations, so we can work on that.

    I want the user to enter an equation with form like: (3*(2 +4)) allowing spaces and (,),[,]. Pretty much any math expression that makes use of parenthesis. I also want to be able to make a char[] or string[] e.g.= {3,2,4}.
    It would be bonus if I could have the program deal with the input in the same way as a IDE shows if there are errors as you type.

    So I have figured that there are many ways to do it, but the methods I know should work are long, tedious, and inefficient program.

    I don't know if I should just turn the scanner into a string. And then work from the string.
    I think working straight from the scanner might be better because it has next() type of methods, but I don't how to use. And I don't understand the whitespace, or using pattern if it could help.

    Working from string: I've figured out how to remove the ( ) and spaces using replace() and replaceAll().
    Why does equation = equation.replace( "(", "" ); but not equation = equation.replaceAll( "(", "" );

    One method I thought of to remove just the letters from the string and put into the array. I would have to create a if statement for every letter. Which i don't want to write 52 if statements. This all of what my problem is I have to deal with long for loops and tedious if statements. I want to learn easier ways.

    I created a method to count left parenthesis and right parenthesis. If they are not equal it allows the equation not run and report error. The number from the parenthesis I think will help go through order of the operations. But, all the things I have talked about doesn't get close to actually apply the operations.
    My plan is to have a static method for each operation.
    for the math on e.g.
    static int plus(int a, int b){
    return a+b;
    }
    Since I want to make the methods binary. If I have (2+3+4) it will need to know it is the same as (2+(3+4)) and make plus(2,plus(3,4));

    If anyone can give me any help. Explain somethings. More so show me some java methods I haven't found. It is greatly appreciated.
    It was only yesterday I found string.toCharArray(); with saved a for loop.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Parenthetical input

    So you want to make a lexer/parser? That's a rather difficult (and interesting) topic. While it's possible to write a parser by hand, it is definitely not recommended for anything but the most basic of parsers. Instead I would recommend using a tool called ANTLR. It lets you generate a lexer and parser using a specified grammar.

    There's a very helpful video tutorial series on how to use ANTLR here.

Similar Threads

  1. Getting input
    By BuhRock in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 22nd, 2010, 10:30 AM
  2. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM
  3. Input Validation
    By nic in forum AWT / Java Swing
    Replies: 4
    Last Post: November 18th, 2009, 10:54 AM
  4. Values of Input
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 8th, 2009, 03:46 AM
  5. Program to mask input
    By sah in forum Java Theory & Questions
    Replies: 1
    Last Post: January 26th, 2009, 06:43 PM

Tags for this Thread