Results 1 to 9 of 9
- 02-22-2011, 09:51 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 24
- Rep Power
- 0
Why wont my boolean variable change?
Hello I'm making a calculater and I have some problem with my booleans.
When I press a number the boolean 'opera' should be set to true so that my program can register if I press +,- or *. But there is a problem, the code in getNrAction will run and register and print the first number i press, but the boolean wont change to true. Anyone have any ideas of what is wrong?
PHP Code:public class Calculate { private String _operator; private String _nr; private String _equal; private int frtInt; private int scdInt; boolean frtNr = true; boolean scdNr = false; boolean opera = false; public Calculate() { } //when '*', '/'or '+' is pressed public void getOperAction(String text) { if (opera == true) { _operator = text; scdNr = true; opera = false; System.out.println(_operator); } if(opera == false) System.out.println("FAILURE TEST"); } // when a number-button is pressed public void getNrAction(String text) { _nr = text; int intNr = Integer.parseInt(_nr); if(frtNr == true ) { frtInt = intNr; System.out.println(frtInt); opera = true; frtNr = false; } if (scdNr == true) { scdInt = intNr; System.out.println(scdNr); } } // when '='-button is pressed public void getEqualAction(String text) { _equal = text; System.out.println(_equal); } // public String giveNrError() // { // return errorNr; // } }
- 02-22-2011, 10:13 PM #2
Obviously the method where the boolean is changed never gets called. Since you did not post any code that does call that method, or is supposed to call that method, nobody can help you.
- 02-22-2011, 10:17 PM #3
Senior Member
- Join Date
- Feb 2010
- Location
- Waterford, Ireland
- Posts
- 748
- Rep Power
- 4
Look you are setting opera equal to false and then the next if statement will equate to true because of it:
Java Code:public void getOperAction(String text) { if (opera == true) { _operator = text; scdNr = true; [COLOR="DarkOrange"] opera = false;[/COLOR] System.out.println(_operator); } [COLOR="DarkOrange"] if(opera == false)[/COLOR] System.out.println("FAILURE TEST"); }
- 02-22-2011, 10:19 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 24
- Rep Power
- 0
The class for the Number-buttons
PHP Code:import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class nrButton extends JButton implements ActionListener { String _nr; Calculate _calcApp = new Calculate(); JButton[] _nrButtonList = new JButton[10]; private JButton _1, _2, _3, _4, _5, _6, _7, _8, _9, _0; public nrButton() { super(); _1 = new JButton("1"); _2 = new JButton("2"); _3 = new JButton("3"); _4 = new JButton("4"); _5 = new JButton("5"); _6 = new JButton("6"); _7 = new JButton("7"); _8 = new JButton("8"); _9 = new JButton("9"); _0 = new JButton("0"); _1.addActionListener(this); _2.addActionListener(this); _3.addActionListener(this); _4.addActionListener(this); _5.addActionListener(this); _6.addActionListener(this); _7.addActionListener(this); _8.addActionListener(this); _9.addActionListener(this); _0.addActionListener(this); } public JButton[] getNrButton() { _nrButtonList[0]=_1; _nrButtonList[1]=_2; _nrButtonList[2]=_3; _nrButtonList[3]=_4; _nrButtonList[4]=_5; _nrButtonList[5]=_6; _nrButtonList[6]=_7; _nrButtonList[7]=_8; _nrButtonList[8]=_9; _nrButtonList[9]=_0; return _nrButtonList; } public void actionPerformed(ActionEvent e) { _nr = e.getActionCommand(); _calcApp.getNrAction(_nr); } }
and the class for the operator-buttons
PHP Code:import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; public class operButton implements ActionListener { private JButton _add, _sub, _multi; JButton[] _operButtonList = new JButton[3]; private String _operator; Calculate _calcApp = new Calculate(); public operButton() { super(); } public JButton[] getOperButton() { _add = new JButton("+"); _sub = new JButton("-"); _multi = new JButton("*"); _add.addActionListener(this); _sub.addActionListener(this); _multi.addActionListener(this); _operButtonList[0] = _add; _operButtonList[1] = _sub; _operButtonList[2] = _multi; return _operButtonList; } public void actionPerformed(ActionEvent e) { _operator = (e.getActionCommand()); _calcApp.getOperAction(_operator); } }
They get called, I've tried with removing the booleans, and both works. But the booleans wont change when the method runs, or the booleans somehow doesnt make the whole method run.
- 02-22-2011, 10:26 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 24
- Rep Power
- 0
Now I have
PHP Code:public void getOperAction(String text) { if (opera == true) // this doesnt work getNrAction doesn't change opera to true. { _operator = text; System.out.println(_operator); } else if(opera == false && frtNr == false) //this doesnt work becasue getNrAction doesn't change frtNr to false.. the booleans wont change when I runt the getNrAction-method. { System.out.println("FAILURE TEST"); } scdNr = true; opera = false; }
-
Every time you create a Calculate object by calling new Calculate, you create a separate and distinct object that is unrelated to the other Calculate objects except that they came from the same class. Perhaps your problem is that you have two or more of these objects (check above how often you see "new Calculate()") when you only want one.
- 02-22-2011, 10:32 PM #7
Member
- Join Date
- Nov 2010
- Posts
- 24
- Rep Power
- 0
-
If you want the presses of the two buttons to interact within Calculate, you'd better have one object only. Perhaps initialize the one Calculate object in whatever class that initializes nrButton and operButton, and do it before creating the button classes. Then give the button classes a constructor parameter that accepts a Calculate object and pass the same object into both button classes. In the constructors set a class Calculate field to refer to the object that was passed in via the parameter.
- 02-22-2011, 10:43 PM #9
Member
- Join Date
- Nov 2010
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
can i listen to variable change in diffrent class
By gloomy1991 in forum New To JavaReplies: 2Last Post: 01-08-2011, 01:50 AM -
Why wont this compile?
By Student101 in forum New To JavaReplies: 8Last Post: 11-18-2010, 05:33 AM -
wont validate
By karq in forum XMLReplies: 1Last Post: 10-07-2010, 05:33 PM -
uploaded image wont display if i change filename
By schenker in forum Java ServletReplies: 12Last Post: 06-11-2010, 11:13 AM -
why wont it compile
By bje98f in forum Advanced JavaReplies: 1Last Post: 04-23-2009, 10:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks