permaculture playing cards*
The moose likes Beginning Java and the fly likes calculator - logic problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "calculator - logic problem" Watch "calculator - logic problem" New topic
Author

calculator - logic problem

David Noakes
Greenhorn

Joined: Nov 26, 2004
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.
answer = 0;
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;
answer = lastNum + 0;
typeHere.setText("");
break;
case 12 : lastNum = answer + currNum - lastNum;
answer = 0 + lastNum;
typeHere.setText("");
break;

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

lastNum = 0;
}
}
}
else {
switch (currOp) {
case 11 : answer = answer + currNum + lastNum;
typeHere.setText("" + answer);
break;
case 12 : answer = answer + currNum - lastNum;
typeHere.setText("" + answer);
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
Thanks for reading through this.
David.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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:



JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
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

Joined: Dec 10, 2001
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!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
David Noakes
Greenhorn

Joined: Nov 26, 2004
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

Joined: Dec 06, 2001
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


Java API Documentation
The Java Tutorial
David Noakes
Greenhorn

Joined: Nov 26, 2004
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: calculator - logic problem