Please help, how to convert a String to an arithmetic operation?

I have a string, for example "4+3-2" and I want to have the result of these operations, how can I do it?

The numbers of the string are inserted by the user, so I don't know them unless I run the program, and they come all together from a textfield.

Could you help me please?:confused::-? Thanks

Re: Please help, how to convert a String to an arithmetic operation?

That sounds like what a compiler does: takes Strings and converts them to operations that a computer can do.

If the full expression is always the same, 3 numbers with two operators + and -: <number>+<number>-<number> then it gets a lot easier.

Where are you having problems?

Re: Please help, how to convert a String to an arithmetic operation?

The first thing you have to learn how to do is splitting the string up into its logical parts.

If you have the string "111+2222 - 33" you have to split it up to a list / array / collection of strings like these:

"111"

"+"

"2222"

"-"

"33"

Then you can start working from here.

Try to do this by yourself first, read the documentation for the String class as it provides many useful methods. The Documentation for the Character class might also be useful to you.

If you are still having problems ask them here in this thread and I am sure somebody will help you out.

Re: Please help, how to convert a String to an arithmetic operation?

thanks for the answers, but maybe I didn't explain well my problem. I don't know which is the string, I ask a user to write the operations in a textfield, so I could get "4+3-2" s well as "5-4-3+44". My question was: does it exist a method that converts every string I get from the textfield to operations and gives me the result?

However I know how to split a String, and I tried that too but without success..

Re: Please help, how to convert a String to an arithmetic operation?

Quote:

does it exist a method that converts every string I get from the textfield to operations and gives me the result?

No, that's the logic you need to write.

Quote:

I tried that too but without success.

Show what you tried with the results, and we can go from there.

Re: Please help, how to convert a String to an arithmetic operation?

Well, I'm not really practised with java so probably you will find my code naive or worse... just suggest a solution for my problem :)

I'm trying to write a calculator program, with JButtons and a JLabel, and I tried to solve the problem by splitting the operation in different numbers as the user presses an operator (+,-,*,/).

For example:

Code :

if(ref.plus.isFocusOwner())
{
add=true;
String txt=ref.lab.getText();
num1=Double.parseDouble(txt);
ref.lab.setText(txt+"+");
}

and when the user presses equals:

Code :

if(ref.equals.isFocusOwner())
{
txt1=String.valueOf(num1);
String txt2=ref.lab.getText();
String subtxt2=txt2.substring(txt1.length()+1);
num2=Double.parseDouble(subtxt2);
if(add)
{
ref.lab.setText(String.valueOf(num1+num2));
}
if(subtract)
{
ref.lab.setText(String.valueOf(num1-num2));
}
if(multiply)
{
ref.lab.setText(String.valueOf(num1*num2));
}
if(divide)
{
ref.lab.setText(String.valueOf(num1/num2));
}
add=false;subtract=false;divide=false;multiply=false;
}

This way it works fine, but when it does the first operation, it gives me problems with the second number.

If I write "12+12", it does nothing, if I write "12+122" it does just "12+002". The problem could be that when it converts to double it adds to the number the ".0" which increases the "txt1.length()". In the second or further operation, it works, because the number I start with is already a double(Ex:12.0+12=24.0).

I hope I made myself clear this time, and thanks again for your help (insults for naive code are not welcome)

Re: Please help, how to convert a String to an arithmetic operation?

We don't insult here - not more than once, anyway. If you believe you've been insulted, report the insulting post using the triangle-exclamation icon at the bottom of the post.

I didn't trace your code to determine why you're seeing the behavior you're reporting, but I suspect your logic is not correctly finding the next number entered after the operator.

I recommend parsing the whole string **after** the user has completed entering the equation. Parse the string into operands and operators, perhaps sequentially from left to right, from that construct the equation, and then perform the necessary calculations to determine the answer. You may not know stacks yet, but using stacks to store the operands and operators is a common equation parsing technique. You can do similarly without stacks by processing each element of the equation as it is parsed.

You may find other ideas Googling something like 'java equation parser'.

Re: Please help, how to convert a String to an arithmetic operation?

Are the operators only + and -? That will make it easier because they have the same precedence. If other operators with different precedence's (like * and /) are allowed it gets much harder.