
Java novice that needs to learn a few things!
Hi, I'm relatively a novice at Java programming, as I've only taken a few programming courses at college, but have been trying to write my own programs on my own for a few months now to get more practice.
There's something that I haven't been able to figure out yet, and it's really making my basic math program tough to finish.
Say I have a basic math function. Say, ((x+2)*3), or anything similar to that. Now, I have this value inputted by the user as a string. How would I be able to code my program, so that the function could be evaluated (using an arbitrary value for x that the user can also input.) I'm sure I'm overlooking something relatively simple, but how would I evaluate a mathmatical function that is originally a string.
I appreciate any help that I can get.

Do you mean how do you convert a String to an int?
Code:
int x = String.valueOf("123");
If you're using the Scanner class for input, you can just retrieve it as an int:
Code:
import java.util.Scanner;
// ...
Scanner scanner = new Scanner(System.in);
int x = scanner.nextInt();

Sorry, I don't think I clarified that right.
What I mean, is that if the user enters any mathematical function that involves variables and whatnot, how would I use that evaluate that function later.
For example:
Enter any function of x:
User enters (x+2)(x+3) < this is as a string.
Enter any value for x:
User enters 3.
And the the calculated result would be (3+2)(3+3) = 30.
That's basically what I'm trying to do.

Originally Posted by TheBigG753
Sorry, I don't think I clarified that right.
What I mean, is that if the user enters any mathematical function that involves variables and whatnot, how would I use that evaluate that function later.
For example:
Enter any function of x:
User enters (x+2)(x+3) < this is as a string.
Enter any value for x:
User enters 3.
And the the calculated result would be (3+2)(3+3) = 30.
That's basically what I'm trying to do.
So you just want to store the value?
Code:
import java.util.Scanner;
public class Demo {
static public void main(String[] args) {
System.out.print("Enter any value for x: ");
int x = new Scanner(System.in).nextInt();
int y = (x + 2) * (x + 3);
System.out.println(y);
}
}
Output from this program:
Code:
Enter any value for x: 3
30

The way I would do it, would involve using a stack to keep hold of the integer values and another stack for the operators (+, , *, /) and then just keep track of the brackets and do the math that way..
ie: (3+2)(3+3)
1. Remember (
2. Push 3 on stack
3. Push + on another stack
4. Push 2 on stack
5. OK end of brackets pop 3 and 2 from stack and use + from other stack
etc etc..
*Edit* Destin I think he wants the user to enter the whole expression as a string..

Basically, I want this program to work for any function that the user wants to evaluate, and I'm beginning to wonder if it's even possible to do, though I'm sure there's a way somehow. Basically the user can enter this string "x^2," or "x+3" or "((x^2)+(2*x)+3)" and then they are what functions are evaluated for what the user wants to be the value of x. I don't want to have any given function already in the program, I want the function that the user inputs as a string, to somehow be used as any regular operation. How would a string of variables, operators and numbers be converted into an operation itself that can be calculated? That's where I'm stuck. If there's no efficient way of doing this, sorry for the bad question, I just figured that if there wasn't an easier way, I'd be doing unnecessary grunt work trying to convert it over recursively.

Originally Posted by TheBigG753
Basically, I want this program to work for any function that the user wants to evaluate, and I'm beginning to wonder if it's even possible to do, though I'm sure there's a way somehow. Basically the user can enter this string "x^2," or "x+3" or "((x^2)+(2*x)+3)" and then they are what functions are evaluated for what the user wants to be the value of x. I don't want to have any given function already in the program, I want the function that the user inputs as a string, to somehow be used as any regular operation. How would a string of variables, operators and numbers be converted into an operation itself that can be calculated? That's where I'm stuck. If there's no efficient way of doing this, sorry for the bad question, I just figured that if there wasn't an easier way, I'd be doing unnecessary grunt work trying to convert it over recursively.
are you saying that you want to turn the string into a integer to be evaluated ?
if so then ill ask some pros and ill let you know when i find the answer.

Well, first of all, in Java, ^ is a bitwise operation. It does not do "to the power of", you would need Math.pow for that. But onto your question... You would need to write your own function that manipulates Strings... it could get annoying. You'd need to first remove all the spaces, then start evaluating the String. when you see *, you multiply the term before it by the term after it, same as /, +, . You'd need to encorporate the order of operations into your program as well.
I do not know of any way to take a String and turn it into actual Java code.
Hope this is what you're looking for.
Good luck!

It can be done.. I have done something sort of similar "Postfix Calculator"..
There is a bit more to it now, if you want to evaluate ANY expression..
But if you wanna do expressions with () then I think a stacks are the way to go..

Alright, thanks for all of the help. That's basically what I was expecting to have to do, just wanted to make sure I wasn't doing more work than I had to. Again, thanks to everyone.

Originally Posted by Code_Nerd
It can be done.. I have done something sort of similar "Postfix Calculator"..
There is a bit more to it now, if you want to evaluate ANY expression..
But if you wanna do expressions with () then I think a stacks are the way to go..
The only useful thing a Stack could do for you is check if you have enough parentheses.. it wouldn't even be that useful, becuase I'm assuming you're only using '(' and ')', so you could simply count the number of each to check if there is a correct amount.

What method would you use to find the integers and the operators and keep track of them?

What you need is an expresion parser, that can handle mathematical expressions. that si much more difficult than you think, especially for a novice. while studying we had to do a programm that uses a descending parser to parse and evaluate such expressions. under this link you can find all the sources we used. you will see, that is much more complex than you thought.

Originally Posted by destin
The only useful thing a Stack could do for you is check if you have enough parentheses.. it wouldn't even be that useful, becuase I'm assuming you're only using '(' and ')', so you could simply count the number of each to check if there is a correct amount.
U need the stack for sure...You can't just count the number of parantheses and expect them to be well formed at the same time

Originally Posted by Code_Nerd
The way I would do it, would involve using a stack to keep hold of the integer values and another stack for the operators (+, , *, /) and then just keep track of the brackets and do the math that way..
ie: (3+2)(3+3)
1. Remember (
2. Push 3 on stack
3. Push + on another stack
4. Push 2 on stack
5. OK end of brackets pop 3 and 2 from stack and use + from other stack
etc etc..
*Edit* Destin I think he wants the user to enter the whole expression as a string..
The way Code_Nerd suggested is the best way to get it done using the stacks. It would work perfectly, but you'd have to store the operators you which the program can use. This would cater for situations when a symbol used for an operator is not recognised by Java but is commonly used for the the representation of an operation. e.g with the power issue (x^2).
Similar Threads

By Rob Abbe in forum Talk to the Editors
Replies: 44
Last Post: 01132003, 03:57 PM

Replies: 2
Last Post: 12312002, 05:55 PM

By Mike Tsakiris in forum .NET
Replies: 11
Last Post: 10042002, 06:32 PM

By Anonymous in forum Java
Replies: 0
Last Post: 02012002, 12:08 AM

By malikmehmood in forum Careers
Replies: 2
Last Post: 12152000, 05:33 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

Forum Rules

Development Centers
 Android Development Center
 Cloud Development Project Center
 HTML5 Development Center
 Windows Mobile Development Center
