Results 1 to 9 of 9
  1. #1
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default 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;
    //    }
        
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    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.

  3. #3
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    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");
            
        }

  4. #4
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    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.

  5. #5
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by al_Marshy_1981 View Post
    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");
            
        }

    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;
    
    		
    	}

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  7. #7
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.

    I have 2 Calculate objects, one in class nrButton and one in operButton, but thats right? Or can I use one object created in one class and use it in another?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  9. #9
    equal is offline Member
    Join Date
    Nov 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    Oh okey, thans never thought of that. Will try it out.

Similar Threads

  1. can i listen to variable change in diffrent class
    By gloomy1991 in forum New To Java
    Replies: 2
    Last Post: 01-08-2011, 01:50 AM
  2. Why wont this compile?
    By Student101 in forum New To Java
    Replies: 8
    Last Post: 11-18-2010, 05:33 AM
  3. wont validate
    By karq in forum XML
    Replies: 1
    Last Post: 10-07-2010, 05:33 PM
  4. uploaded image wont display if i change filename
    By schenker in forum Java Servlet
    Replies: 12
    Last Post: 06-11-2010, 11:13 AM
  5. why wont it compile
    By bje98f in forum Advanced Java
    Replies: 1
    Last Post: 04-23-2009, 10:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •