Results 1 to 5 of 5
Thread: it doesnt see my 'if' statements
- 06-27-2012, 04:33 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 2
- Rep Power
- 0
it doesnt see my 'if' statements
A stripped down version of my program is in the 2 files copied below. It is a simple calculator like in Windows accessories. The example below only does addition and subtraction. It isn't completely written - never writes the answer to the textbox I haven't got that far yet. The code below is supposed to work as follows: A number is sent to the textbox using button clicks and when '+' button is clicked the displayed string is converted to double. The displayed string remains displayed until the first digit of the next number is entered then the first number is cleared and the second number begins in the display. When '=' is clicked the second displayed number (actually a string) is converted to double and added to the first double number. The operation should be controlled by the 'opFlag' and 'addFlag' settings. It shouls change the textbox when the second number is entered but it does not. The problem apparently is that the 'if' statements that set and test the flags are not being executed. I tried NetBeans debugger to watch the values but I don't know how to use the debugger well enough. I'm hoping someone here can tell me what I'm doing wrong so I can move on. Your comments will be appreciated. TIA Bill S.
THE GUI FILE:
import java.awt.*;
import javax.swing.*;
public class CalcGUIQ2 extends JFrame {
CrunchQ2 crunchNu = new CrunchQ2(this);
// set up row 1
JPanel row1 = new JPanel();
JTextField number1 = new JTextField(30);
// set up row 2
JPanel row2 = new JPanel();
JButton sev = new JButton("7");
JButton ate = new JButton("8");
JButton nin = new JButton("9");
JButton fou = new JButton("4");
JButton fiv = new JButton("5");
JButton six = new JButton("6");
JButton one = new JButton("1");
JButton two = new JButton("2");
JButton tre = new JButton("3");
JButton add = new JButton("+");
JButton sub = new JButton("--");
JButton zro = new JButton("0");
JButton dot = new JButton(".");
JButton equ = new JButton("=");
public CalcGUIQ2() {
super();
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout = new GridLayout(2, 1, 10, 10);
setLayout(layout);
//add listeners
dot.addActionListener(crunchNu);
zro.addActionListener(crunchNu);
one.addActionListener(crunchNu);
two.addActionListener(crunchNu);
tre.addActionListener(crunchNu);
fou.addActionListener(crunchNu);
fiv.addActionListener(crunchNu);
six.addActionListener(crunchNu);
sev.addActionListener(crunchNu);
ate.addActionListener(crunchNu);
nin.addActionListener(crunchNu);
equ.addActionListener(crunchNu);
add.addActionListener(crunchNu);
sub.addActionListener(crunchNu);
FlowLayout layout1 = new FlowLayout(FlowLayout.CENTER, 10, 10);
row1.add(number1);
row1.setLayout(layout1);
add(row1);
GridLayout layout2 = new GridLayout(5, 3, 10, 10);
row2.setLayout(layout2);
row2.add (sev);
row2.add (ate);
row2.add (nin);
row2.add (fou);
row2.add (fiv);
row2.add (six);
row2.add (one);
row2.add (two);
row2.add (tre);
row2.add (zro);
row2.add (dot);
row2.add (equ);
row2.add (add);
row2.add (sub);
add(row2);
pack();
setVisible(true);
}
public static void main(String[] arguments) {
CalcGUIQ2 frame = new CalcGUIQ2();
}
}
THE CALCULATION FILE:
import javax.swing.*;
import java.awt.event.*;
public class CrunchQ2 implements ActionListener{
CalcGUIQ2 gui;
String strng1 = "";
String noChar = "";
String nine = "999999999";
boolean addFlag = false;
boolean subFlag = false;
boolean opFlag = false;
Double operand1 = 0.0;
Double operand2 = 0.0;
public CrunchQ2(CalcGUIQ2 in) {
gui = in;
}
public void actionPerformed(ActionEvent event){
String btn = event.getActionCommand();
// opflag clears display
if (opFlag){
gui.number1.setText(noChar);
opFlag = false;
}
if (btn == "=") {
operand2=Double.parseDouble(strng1);
if (addFlag) {
operand1 += operand2;
addFlag = false;
}
if (subFlag) {
operand1 -= operand2;
subFlag = false;
}
}
if (btn == ".") {strng1 += btn;}
else if (btn == "0") {strng1 += btn;}
else if (btn == "1") {strng1 += btn;}
else if (btn == "2") {strng1 += btn;}
else if (btn == "3") {strng1 += btn;}
else if (btn == "4") {strng1 += btn;}
else if (btn == "5") {strng1 += btn;}
else if (btn == "6") {strng1 += btn;}
else if (btn == "7") {strng1 += btn;}
else if (btn == "8") {strng1 += btn;}
else if (btn == "9") {strng1 += btn;}
gui.number1.setText(strng1);
if (btn == "+") {
addFlag = true;
opFlag = true;
operand1=Double.parseDouble(strng1);}
if (btn == "--") {
subFlag = true;
opFlag = true;
operand1=Double.parseDouble(strng1);}
}
}
- 06-27-2012, 05:09 AM #2
Re: it doesnt see my 'if' statements
Please edit your post, properly format the code with indentations to show nesting levels and wrap the code in code tags. All the statements should NOT start in the first column. It makes it hard to see the logic.
BB Code List - Java Programming Forum
One problem I see: You should use the equals() method to compare Strings, not the == operator.
Java Code:if (btn == "+") { // vs if (btn.equals("+")) {
Last edited by Norm; 06-27-2012 at 05:13 AM.
If you don't understand my response, don't ignore it, ask a question.
- 06-27-2012, 01:19 PM #3
Member
- Join Date
- Jun 2012
- Posts
- 2
- Rep Power
- 0
Re: it doesnt see my 'if' statements
Thanks. I loose indentation when I copy code to the forum page. I have tried the equals() method and that doesnt solve the problem. There is something else wrong.
- 06-27-2012, 01:37 PM #4
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 12
Re: it doesnt see my 'if' statements
Please wrap all the code in [code] and [/code] tags. Otherwise we can't read it, and if we can't read your code, it's very hard for us to help.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 06-27-2012, 02:43 PM #5
Similar Threads
-
Two classes: can someone tell me why this doesnt work?
By katiebear128 in forum New To JavaReplies: 12Last Post: 11-08-2011, 04:08 AM -
Applet doesnt start or it doesnt show
By 3dprogger in forum Java AppletsReplies: 2Last Post: 01-07-2011, 08:03 PM -
Method Doesnt turn negative number 0 and doesnt return 3
By anonb in forum New To JavaReplies: 7Last Post: 09-28-2010, 01:17 AM -
PrintWriter doesnt work :(
By Addez in forum New To JavaReplies: 11Last Post: 01-17-2010, 06:59 PM -
Dll Call doesnt work
By INFACT in forum New To JavaReplies: 1Last Post: 10-04-2009, 10:31 PM
Bookmarks