Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #15625 > unrolled thread
| Started by | bilsch <bilsch01@gmail.com> |
|---|---|
| First post | 2012-06-26 15:49 -0700 |
| Last post | 2012-06-27 03:16 -0700 |
| Articles | 10 — 6 participants |
Back to article view | Back to comp.lang.java.programmer
It doesn't see my 'if' statements bilsch <bilsch01@gmail.com> - 2012-06-26 15:49 -0700
Re: It doesn't see my 'if' statements markspace <-@.> - 2012-06-26 15:52 -0700
Re: It doesn't see my 'if' statements Lew <lewbloch@gmail.com> - 2012-06-26 16:40 -0700
Re: It doesn't see my 'if' statements bilsch <bilsch01@gmail.com> - 2012-06-26 17:22 -0700
Re: It doesn't see my 'if' statements Mike Winter <usenet@michael-winter.me.invalid> - 2012-06-27 02:04 +0100
Re: It doesn't see my 'if' statements Martin Gregorie <martin@address-in-sig.invalid> - 2012-06-26 23:44 +0000
Re: It doesn't see my 'if' statements bilsch <bilsch01@gmail.com> - 2012-06-26 17:18 -0700
Re: It doesn't see my 'if' statements bilsch <bilsch01@gmail.com> - 2012-06-26 17:04 -0700
Re: It doesn't see my 'if' statements markspace <-@.> - 2012-06-26 19:47 -0700
Re: It doesn't see my 'if' statements Roedy Green <see_website@mindprod.com.invalid> - 2012-06-27 03:16 -0700
| From | bilsch <bilsch01@gmail.com> |
|---|---|
| Date | 2012-06-26 15:49 -0700 |
| Subject | It doesn't see my 'if' statements |
| Message-ID | <jsde9p$ebg$1@dont-email.me> |
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 only
does addition and subtraction but it doesn't work. A number is
accumulated in the textbox and when '+' button is clicked the displayed
string is converted to double, the displayed string remains until the
first digit of the next number is entered then the first number is
cleared and the second number begins. When '=' is clicked the second
displayed string (number) is converted to double and added to the first
double number. The operation is controlled by 'opFlag' and 'addFlag'
settings. The problem apparently is that the 'if' statements that set
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 = true){
gui.number1.setText(noChar);
opFlag = false;
}
if (btn == "=") {
operand2=Double.parseDouble(strng1);
if (addFlag = true) {
operand1 += operand2;
addFlag = false;
}
if (subFlag = true) {
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);}
}
}
[toc] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2012-06-26 15:52 -0700 |
| Message-ID | <jsdefq$ek9$1@dont-email.me> |
| In reply to | #15625 |
On 6/26/2012 3:49 PM, bilsch wrote:
> public void actionPerformed(ActionEvent event){
> String btn = event.getActionCommand();
> // opflag clears display
> if (opFlag = true){
This is assignment, not == test. You need two == for equals testing.
If you are using NetBeans, it should have flagged this with a warning.
Mine does.
[toc] | [prev] | [next] | [standalone]
| From | Lew <lewbloch@gmail.com> |
|---|---|
| Date | 2012-06-26 16:40 -0700 |
| Message-ID | <3a22d9ec-dc33-4443-aec2-9dee3bd62dbf@googlegroups.com> |
| In reply to | #15626 |
On Tuesday, June 26, 2012 3:52:41 PM UTC-7, markspace wrote:
> On 6/26/2012 3:49 PM, bilsch wrote:
>
> > public void actionPerformed(ActionEvent event){
> > String btn = event.getActionCommand();
> > // opflag clears display
> > if (opFlag = true){
>
>
> This is assignment, not == test. You need two == for equals testing.
>
> If you are using NetBeans, it should have flagged this with a warning.
> Mine does.
It's also redundant.
if (opFlag)
is all you need.
Otherwise you are either saying 'if (true == true)' which is the exact same logic
as 'if (true)', or 'if (false == true)', which is the same (almost) as 'if (false)'.
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | bilsch <bilsch01@gmail.com> |
|---|---|
| Date | 2012-06-26 17:22 -0700 |
| Message-ID | <jsdjom$fic$1@dont-email.me> |
| In reply to | #15630 |
On 6/26/2012 4:40 PM, Lew wrote:
> On Tuesday, June 26, 2012 3:52:41 PM UTC-7, markspace wrote:
>> On 6/26/2012 3:49 PM, bilsch wrote:
>>
>>> public void actionPerformed(ActionEvent event){
>>> String btn = event.getActionCommand();
>>> // opflag clears display
>>> if (opFlag = true){
>>
>>
>> This is assignment, not == test. You need two == for equals testing.
>>
>> If you are using NetBeans, it should have flagged this with a warning.
>> Mine does.
>
> It's also redundant.
>
> if (opFlag)
>
> is all you need.
>
> Otherwise you are either saying 'if (true == true)' which is the exact same logic
> as 'if (true)', or 'if (false == true)', which is the same (almost) as 'if (false)'.
>
I changed the opFlag and addFlag statements and it STILL doesn't work.
TIA Bill S.
[toc] | [prev] | [next] | [standalone]
| From | Mike Winter <usenet@michael-winter.me.invalid> |
|---|---|
| Date | 2012-06-27 02:04 +0100 |
| Message-ID | <o_sGr.321702$3i1.7177@fx10.am4> |
| In reply to | #15638 |
On 27/06/2012 01:22, bilsch wrote:
>>> On 6/26/2012 3:49 PM, bilsch wrote:
>>>
>>>> public void actionPerformed(ActionEvent event){
>>>> String btn = event.getActionCommand();
>>>> // opflag clears display
>>>> if (opFlag = true){
[snip]
> I changed the opFlag and addFlag statements and it STILL doesn't work.
There's also the subFlag condition (and ideally using the equals method
for comparison as mentioned in <jsd9cf$hha$1@dont-email.me>), but
another, significant problem is to do with your program's logic as you
don't reset the state of the String, strng1, or output the computed value.
After an addition or subtraction operation, you need to reset your
string so that you start building the next operand, not a concatenation
of the first /and/ second operands. You also need to set the JTextField
text to the (type-converted) value of operand1 when requesting the total.
Kind regards,
Mike
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
[toc] | [prev] | [next] | [standalone]
| From | Martin Gregorie <martin@address-in-sig.invalid> |
|---|---|
| Date | 2012-06-26 23:44 +0000 |
| Message-ID | <jsdhh1$qfu$2@localhost.localdomain> |
| In reply to | #15626 |
On Tue, 26 Jun 2012 15:52:41 -0700, markspace wrote:
> On 6/26/2012 3:49 PM, bilsch wrote:
>
>> public void actionPerformed(ActionEvent event){
>> String btn = event.getActionCommand();
>> // opflag clears display if (opFlag = true){
>
>
> This is assignment, not == test. You need two == for equals testing.
>
> If you are using NetBeans, it should have flagged this with a warning.
> Mine does.
As far as I can see the result of your calculation is never written to
gui.number1
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
[toc] | [prev] | [next] | [standalone]
| From | bilsch <bilsch01@gmail.com> |
|---|---|
| Date | 2012-06-26 17:18 -0700 |
| Message-ID | <jsdjg0$e4g$1@dont-email.me> |
| In reply to | #15631 |
On 6/26/2012 4:44 PM, Martin Gregorie wrote:
> On Tue, 26 Jun 2012 15:52:41 -0700, markspace wrote:
>
>> On 6/26/2012 3:49 PM, bilsch wrote:
>>
>>> public void actionPerformed(ActionEvent event){
>>> String btn = event.getActionCommand();
>>> // opflag clears display if (opFlag = true){
>>
>>
>> This is assignment, not == test. You need two == for equals testing.
>>
>> If you are using NetBeans, it should have flagged this with a warning.
>> Mine does.
>
> As far as I can see the result of your calculation is never written to
> gui.number1
>
>
It doesn't go that far. I'm testing for the display to change when the
second string of digits starts getting entered.
[toc] | [prev] | [next] | [standalone]
| From | bilsch <bilsch01@gmail.com> |
|---|---|
| Date | 2012-06-26 17:04 -0700 |
| Message-ID | <jsdin7$a5t$1@dont-email.me> |
| In reply to | #15626 |
On 6/26/2012 3:52 PM, markspace wrote:
> On 6/26/2012 3:49 PM, bilsch wrote:
>
>> public void actionPerformed(ActionEvent event){
>> String btn = event.getActionCommand();
>> // opflag clears display
>> if (opFlag = true){
>
>
> This is assignment, not == test. You need two == for equals testing.
>
> If you are using NetBeans, it should have flagged this with a warning.
> Mine does.
>
Thanks. My stupid mistake. NetBeans doesn't flag it on my system - ???
I have now fixed those errors and it STILL doesn't work.
[toc] | [prev] | [next] | [standalone]
| From | markspace <-@.> |
|---|---|
| Date | 2012-06-26 19:47 -0700 |
| Message-ID | <jsds81$mde$1@dont-email.me> |
| In reply to | #15636 |
On 6/26/2012 5:04 PM, bilsch wrote:
> On 6/26/2012 3:52 PM, markspace wrote:
>> On 6/26/2012 3:49 PM, bilsch wrote:
>>
>>> public void actionPerformed(ActionEvent event){
>>> String btn = event.getActionCommand();
>>> // opflag clears display
>>> if (opFlag = true){
>>
>>
>> This is assignment, not == test. You need two == for equals testing.
>>
>> If you are using NetBeans, it should have flagged this with a warning.
>> Mine does.
>>
> Thanks. My stupid mistake. NetBeans doesn't flag it on my system - ???
>
> I have now fixed those errors and it STILL doesn't work.
If the NetBeans debugger doesn't work for you, try print statements
instead. Print out the values of variables as you change them or before
testing them, to ensure the values are what you think they are.
> public void actionPerformed(ActionEvent event){
> String btn = event.getActionCommand();
> // opflag clears display
System.out.println( "opFlag="+opFlag );
> if (opFlag = true){
> gui.number1.setText(noChar);
> opFlag = false;
> }
....
> if (btn == "+") {
> addFlag = true;
> opFlag = true;
System.out.println( "addFlag and opFlag = true" );
> operand1=Double.parseDouble(strng1);}
Now you can "see" what opFlag is at the beginning of each invocation of
actionPerformed(). BTW this style of adding print statements is a very
venerable but also a very efficient way of debugging code. It would be
even better to use the logger, but one complication at a time.
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2012-06-27 03:16 -0700 |
| Message-ID | <r5nlu794ai9eac8l036vg6e8138lgnrufo@4ax.com> |
| In reply to | #15625 |
On Tue, 26 Jun 2012 15:49:29 -0700, bilsch <bilsch01@gmail.com> wrote, quoted or indirectly quoted someone who said : >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 only >does addition and subtraction but it doesn't work. A number is >accumulated in the textbox and when '+' button is clicked the displayed >string is converted to double, the displayed string remains until the >first digit of the next number is entered then the first number is >cleared and the second number begins. When '=' is clicked the second >displayed string (number) is converted to double and added to the first >double number. The operation is controlled by 'opFlag' and 'addFlag' >settings. The problem apparently is that the 'if' statements that set >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. I have not studied your program, but it sounds from your general complaint like the problem might be you don't understand that nothing gets painted until after an event handler returns. You may not appreciate why event handlers cannot dither in the least. They have to do i tiny bit of work and return, or else they will gum up the entire JVM which an only process one Swing event at a time. I suggest some background reading on events. e.g. http://mindprod.com/jgloss/event11.html -- Roedy Green Canadian Mind Products http://mindprod.com When you get stuck trying to solve a computer program: 1. Go into the kitchen and make coffee. 2. If that fails, go for a walk. 3. If that fails, take a nap. Why? To avoid being swamped with details, to see the big picture, to allow in some random noise to kick you out of your thinking rut.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web