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 14 of 14

Thread: how to make a program that does the following arithmetic expressions ?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Daring
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to make a program that does the following arithmetic expressions ?

    how to make a program that does the following arithmetic expressions?
    x+ y , x-y ، x/y ، x*y ، x%y , x > y , x < y
    i would like to make the user to inputs the expression and program to show the answer.
    here is an example:
    Hci49792.jpg


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to make a program that does the following arithmetic expressions ?

    If your expressions are only 2 operands and an operator, then you should be able to parse the three parts of the expression and evaluate it. If the expression is more complicated, You need to create/find an expression parser.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Daring
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to make a program that does the following arithmetic expressions ?

    Quote Originally Posted by Norm View Post
    If your expressions are only 2 operands and an operator, then you should be able to parse the three parts of the expression and evaluate it. If the expression is more complicated, You need to create/find an expression parser.
    how to parse the three parts ,, individually x,<,y
    can u give an example from 1 of the expressions above

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to make a program that does the following arithmetic expressions ?

    There are several ways to do it. I have only used a couple and probably not the easiest ones to use.
    The easiest way would be if there were spaces delimiting each of the three parts, then you could use the String class's split() method. That would be the easiest.
    Another would be to use the String class's charAt() method to look at each character at a time and find the three substrings for the three parts.
    Another way is the StreamTokenizer class. There will be some more to learn to get this one to work.
    Another way could be to use regular expressions. I'm not very good with them, but I think they could provide a way to separate the String into its three parts.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Daring
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to make a program that does the following arithmetic expressions ?

    Quote Originally Posted by Norm View Post
    There are several ways to do it. I have only used a couple and probably not the easiest ones to use.
    The easiest way would be if there were spaces delimiting each of the three parts, then you could use the String class's split() method. That would be the easiest.
    Another would be to use the String class's charAt() method to look at each character at a time and find the three substrings for the three parts.
    Another way is the StreamTokenizer class. There will be some more to learn to get this one to work.
    Another way could be to use regular expressions. I'm not very good with them, but I think they could provide a way to separate the String into its three parts.
    how to do it with the string class split() method ?
    can you do an example for me ?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to make a program that does the following arithmetic expressions ?

    Do a Search on the forum for code samples. Or use Google.

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Daring
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to make a program that does the following arithmetic expressions ?

    i know to do a split method, but after splitting the parts how do i take each and parse ?
    that's what i'm not getting till now

  8. #8
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: how to make a program that does the following arithmetic expressions ?

    Take a look at some methods in Integer class, particularly valueOf for retrieving the numbers, then get the particular operator using the methods Norm already talked about, and print the returned number.

    Note that making a fullblown effective calculator requires a lot of work if the numbers get large or there are more than two numbers, but I'm certainly happy to see you doing it . However there is no one-line way to solve arithmetic equations in java. What I have seen however is people using command prompt or something to call an OS calculator, but I'm not sure how they did that.

    Integer (Java Platform SE 7 )
    Last edited by Tjstretch; November 18th, 2011 at 04:49 PM.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to make a program that does the following arithmetic expressions ?

    When you parse: x + y
    you get the three parts of the expression.
    Sorry, I'm confused about where your problem is. If you have split the expression into its three parts, then you can evaluate it directly.

    By parsing I mean splitting the expression up into its parts: operands and operator.

  10. #10
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Daring
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to make a program that does the following arithmetic expressions ?

    Quote Originally Posted by Norm View Post
    When you parse: x + y
    you get the three parts of the expression.
    Sorry, I'm confused about where your problem is. If you have split the expression into its three parts, then you can evaluate it directly.

    By parsing I mean splitting the expression up into its parts: operands and operator.
    what i mean by parsing is like when using JOptionPane.showInputDialog to take numbers from the user and then parsing them into an integer or double,
    since i'm going to use a string and split it into 3 parts ( how to declare each part as a variable )

  11. #11
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: how to make a program that does the following arithmetic expressions ?

    Do you want to do it like

    Popup appears: "Enter first number"
    User enters 30.
    Popup appears: "What operator?"
    User enters +
    Popup appears: "Enter second number"
    User enters 26
    Popup appears: "56"
    because that would be easy, however that isn't what your pictures show.

    Again, to find an Integer or Double, use there valueOf methods

    Integer (Java Platform SE 7 )
    Double (Java Platform SE 7 )

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: how to make a program that does the following arithmetic expressions ?

    The valueOf methods return objects, the parseXXX methods return primitives.

  13. #13
    Junior Member
    Join Date
    Nov 2011
    Posts
    17
    My Mood
    Daring
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: how to make a program that does the following arithmetic expressions ?

    ok i got it now ,, thanks for the big help Norm & Tjstretch

  14. #14
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: how to make a program that does the following arithmetic expressions ?

    @Norm Yeah but it sounds coolor on valueOf, parse sounds all offiicial Go autounboxing!

Similar Threads

  1. simple arithmetic problem
    By nadrii in forum What's Wrong With My Code?
    Replies: 15
    Last Post: October 26th, 2011, 09:35 PM
  2. Help with modifying a program for evaluating arithmetic expressions
    By rockout341 in forum Object Oriented Programming
    Replies: 5
    Last Post: October 13th, 2011, 11:01 AM
  3. how do i make my program to do......
    By andys in forum Object Oriented Programming
    Replies: 6
    Last Post: November 29th, 2010, 07:44 AM
  4. how do i make my program to....
    By andys in forum Object Oriented Programming
    Replies: 2
    Last Post: November 26th, 2010, 10:31 AM
  5. Arithmetic’s Game ??
    By holy crab in forum Algorithms & Recursion
    Replies: 0
    Last Post: April 12th, 2010, 04:27 AM