i am making a calculator in swing and need help with some logic.

so some of button my cal will have:
0,1,2,3,4,5,6,7,8,9,dot, mult,divide,mod, equal,sqrt, ln,log,sin,cos,tan, ^, exp, fractor, etc......

bc i have alot of buttons i need a good way to compute them.

here what i am thinking:
first user can hit as many numbers they want ex 3847.
than i can store in

i = 0;
String s[i] = "3847.5"

than when they hit op buttons than i can do i++ and add the op ex:

String s[i] = "3847.5 +"

when they hit equal sign than i can have a function that compute the string.
only problem i see is there will be alot of errors to check for ex:

"* 3 / 3" error: *
"0 / 3"   error: zero
"8.3.3 * 3" error: dot
"8.3 ++ 3" error: add

these are just some of the cases that i can think of but iam sure there are more.

if no error than compute left to right but computing might be problem too. bc i have to mult than divde than add.

String[] = "343 * 34.0 + 3 \ 3.2"

you guys have any better ways?

have three states (variables) : str1 ,str2 , op;
create a textfeild tf;
set an accelerator to it for the "ENTER" key event, see this and [this](http://docs.oracle.com/javase/7/docs/api/javax/swing/KeyStroke.html#getKeyStroke(java.lang.Character, int))
now , when you type something in tf , and press enter , text of tf gets stored as str1;
when you press a math button ( + - / etc) that operand gets stored as op;
then when you type in the 2nd number in tf , that gets stored into str2;
the block which stored str2 will also have a test : if both str1 and str2 are valid , and an operand has been entered : doCalc();
doCalc() : something that takes in two strings str1 and 2 , casts them as integers , operates them with the entered operand , and store the result as str1.

and the cycle continues.

this was a quickie , might(and probably will) need further refinements.

You could use a boolean flag to mark if the decimal point button has been pressed or not.

Instead of programmming like this why cant you do like this...

void main()
String operator=""; 
String num1;
String num2;
String result;
if add_button is pressed

Likewise same for all other math functions.

And for equalto

if equal_button onclick()
//alert user
int res=num1+num2;

My syntax may be wrong...But I am ask you about the logic used.

Have a happie coding...:-D

you are mixing up arrays and single Strings.
also, you should initialize your array once, not each time you access it.

thanks guys for your idea but i feel like that wont work. or atleast I am missing some thing.

user enter this in calculator: 2 x 4 + 2 / 3
if i do it your way than it be like:
user enter 2x4= than i compute, 8.
than user enter +2= than i compute, 10.
than user enter /3= than i compute, final answer = 3.3..
but that is wrong answer. it should be
2*4+2/3 = 8+0.7 = final answer 8.7

what iam trying to say is i cant compute until user has enter whole problem. than i see the operation order than i compute.

Find out all about a recursive descent parser.

on what basis do you want to compute your answer?

I mean

either using operator precedence or by normal computaion?

missed this in your previous reply, since I was entering a reply myself.
found quite a big error in your logic (related to the syntax)


purely talking about syntax, there's nothing wrong there, it'll compile and run, but there are two quite important logical errors there.

purely talking about syntax, there's nothing wrong there, it'll compile and run, but there are two quite important logical errors there.

What are the errors my friend?

I need to correct myself about that error and i must avoid that in future.

Thank you.

operator = "add"
this is an assignment of value, not a comparison. (actually, since it's not a boolean, I guess it is a syntax error, but most do this with booleans, like 'if ( myBoolean = true )' so I just mentioned it here.
is a very 'dangerous' way to compare Objects, unless we're talking about Singleton/enum/.. one of those special types.
it'll compare the reference of the object, not the actual value.