# calculator - logic problem

David Noakes
Greenhorn
Posts: 3
hey, my first post here. I am learning java atm and have been assigned a calculator project at this stage I have made a method called pushButton that responds to any of my GUI buttons being pushed. i have a logic problem atm in that I cannot seem to add and then substract without silly answers coming out. I'll post my code here from the pushButton() method. If you have any suggestions as to how I can counter the problems please respond. Thanks!
public void pushButton(int in) {
if (in == 15) { // in == 15 is code for CE button on the calculator interface.
currNum = 0;
lastNum = 0;
typeHere.setText("0");
}
else {
if (eqUsed == false) { // boolean variable eqUsed, is set to true only of = button is pushed.
if (calcText == "0") {
typeHere.setText(""); //typeHere is name of a text box.
status = "FIRST"; // status variable starts on FIRST and is changed to NEXT for second num in order to store the operator.
}
if (status == "FIRST") {
if (opUsed == false) {// is set to true only if an operator button is pushed
calcText = typeHere.getText() + in;
currNum = Float.parseFloat(calcText);
typeHere.setText(calcText);
System.out.println("currNum : " + currNum);
}
else {
status = "NEXT";
switch (in) {
case 11 :currOp = 11;
typeHere.setText("");
break;
case 12 :currOp = 12;
typeHere.setText("");
break;
}
}
}
else if (status == "NEXT") {
if (opUsed == false) {
calcText = typeHere.getText() + in;
lastNum = Float.parseFloat(calcText);
typeHere.setText(calcText);
System.out.println("lastNum : " + lastNum);
}
else {
switch (currOp) {
// in == 11 is + ... in == 12 is -
case 11 :lastNum = answer + currNum + lastNum;
typeHere.setText("");
break;
case 12 : lastNum = answer + currNum - lastNum;
typeHere.setText("");
break;

}
status = "FIRST";
switch (in) {
case 11 :currOp = 11;
break;
case 12 : currOp = 12;
break;
}

lastNum = 0;
}
}
}
else {
switch (currOp) {
break;
break;
}
currNum = 0;
lastNum = 0;
eqUsed = false;
status = "FIRST";
}
}
}

sorry about the messy code, the format does not copy nicely into this forum so I had to left align everything! if you are interested in having a look at the full code please email me at davidn@mailbox.co.za and I could mail u back the .java file
David.

Marilyn de Queiroz
Sheriff
Posts: 9065
12
Welcome to Javaranch, David N

Your code will be easier to read if you use the "code" button (next to the smilies) when you post. For example:

Marilyn de Queiroz
Sheriff
Posts: 9065
12
It looks like the "status" variable is a String. I recommend that you use the .equals() method of the String class rather than using "==" for comparing your Strings.

Also, it looks as though you are trying to add a String and an int. You can do that but you won't get the sum of the value of the String and the int, you'll get a String -- the original String (typeHere.getText()) with the int stuck on the end. For example, if your getText() method returns '2' and "in" is '6', "calcText = typeHere.getText() + in;" will return '26'.

[ November 26, 2004: Message edited by: Marilyn de Queiroz ]

By the way, Please adjust your display name to meet the JavaRanch Naming Policy. A last initial doesn't quite fit the standard. You can change it here.
[ November 26, 2004: Message edited by: Marilyn de Queiroz ]

Dirk Schreckmann
Sheriff
Posts: 7023
David N,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

We request something more than a single letter for the displayed last name.

Thanks Pardner! Hope to see you 'round the Ranch!

David Noakes
Greenhorn
Posts: 3
Hi again. Thanks for your guys replies and sorry about the name. Had a little internet trouble recently so I didn't even get a chance to read them I'm afraid. But, in the meantime I reworked my code and realised that I was doing some crazy things before! In case you're interested, this is the new code and it works like a dream.

Cheers.
David.

Layne Lund
Ranch Hand
Posts: 3061
Congratulations! It feels great when your program finally works, eh?

If you don't mind, I have a few comments on your programming style. First of all, 4 spaces for indentation are quite common. Tabs are typically frowned upon, from my experience. Also when you post here, it will help decrease the length of each line and help avoid horizontal scrolling.

Second, a common convention for switch statements is something like this:

Note that each case label is on its own line. The code executed for that case is indented one level.

If you are interested, Sun has published a document describing their recommended coding conventions for the Java language. It's probably not too important that you follow every detail given in this document. The important thing, imho, is to use the conventions that you feel comfortable with consistently. With that said, there are a few standards that will probably be mentioned here at the Ranch if you don't abide by them. In particular, naming conventions seem to be important to most programmers, so pay careful attention to those when you read the above doc.

Keep Coding!

Layne

David Noakes
Greenhorn
Posts: 3
Thanks for the tips, Layne. I agree that your switch case example is far more readable than mine. I try to use the Sun standards wherever possible, unfortunately where I'm currently studying we have to follow the programming standards of my college. The switch case how I set it out is one of those standards. I think I may drop a suggestion at reworking the standards to meet those international standards such as Sun's.
Cheers.