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
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.
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
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.
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.
Do a Search on the forum for code samples. Or use Google.
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
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 15:49.
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.
Do you want to do it like
because that would be easy, however that isn't what your pictures show.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"
Again, to find an Integer or Double, use there valueOf methods
Integer (Java Platform SE 7 )
Double (Java Platform SE 7 )
The valueOf methods return objects, the parseXXX methods return primitives.
ok i got it now ,, thanks for the big help Norm & Tjstretch
@Norm Yeah but it sounds coolor on valueOf, parse sounds all offiicial Go autounboxing!