Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Variables not changing on action.

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Variables not changing on action.

    So i'm new to java and for one of our assignments we have to make an application that can either calculate the area of a triangle, or volume of a box. Everything in my code works but only if I change variables within the code itself, ill post the important bits and hopefully I can get some of your input.

    public class Week05 {
    	boolean tri;
            private void initialize() {
                 btnTriangle.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				tri = true;
    				lblTitle.setText("Area of Triangle");
    				btnTriangle.setEnabled(false);
    				btnBox.setEnabled(true);
    				lblA.setText("Enter S1");
    				lblB.setText("Enter S2");
    				lblC.setText("Enter S3");
    				txtA.setEditable(true);
    				txtB.setEditable(true);
    				txtC.setEditable(true);
    			}
    		});
    		btnTriangle.setBounds(70, 45, 89, 23);
    		frame.getContentPane().add(btnTriangle);
     
    		btnBox.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				tri = false;
    				lblTitle.setText("Volume of a Box");
    				btnBox.setEnabled(false);
    				btnTriangle.setEnabled(true);
    				lblA.setText("Enter l");
    				lblB.setText("Enter w");
    				lblC.setText("Enter h");
    				txtA.setEditable(true);
    				txtB.setEditable(true);
    				txtC.setEditable(true);
    			}
    		JButton btnNewButton = new JButton("Calculate");
    		btnNewButton.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				double a, b, c;
    				a = Double.parseDouble(txtA.getText());
    				b = Double.parseDouble(txtB.getText());
    				c = Double.parseDouble(txtC.getText());
    				while ( a <= 0 || b <= 0 || c <= 0){
    					txtOutput.setText("Values are less than 0");
    					return;
    				}
    				if(tri = true){
    					Triangle area = new Triangle( a, b, c);
    					txtOutput.setText("The area of the Triangle is" + area.getArea());
    				}
    				else if(tri = false) {
    					Box volume = new Box( a, b, c);
    					txtOutput.setText("The volume of the Box is" + volume.getVolume());
    				}
    			}
    		});

    the problem I seem to be having is that my variable tri never changes to false, or if I switch boolean expressions it never changes true on the action of pressing the button for triangle or box.
    Last edited by scrotty544; November 13th, 2012 at 05:43 PM. Reason: solved


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default Re: Variables not changing on action.

    Does this even compile? I see a lot of problems that the compiler *should* complain about.
    But your problem is with this statement:
    if(tri = true){...
    }
    else if(tri = false)...

    A single-equals is an assignment, a double-equals is a comparison. So you need this instead:
    if(tri == true){...
    }
    else if(tri == false)...

    If statements look for conditionals (binary results of either true or false), and a boolean is a conditional, so this is also acceptable:
    if(tri) {...
    }
    else if(!tri)...
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  3. The Following User Says Thank You to aussiemcgr For This Useful Post:

    scrotty544 (November 13th, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Variables not changing on action.

    this was only part of the code that was giving me grief in the application. i actually saw that about a half an hour of staring at it, the == was my only problem and thanks anyways.

Similar Threads

  1. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 07:34 PM
  2. Changing al instance variables at once?
    By Swen in forum Java Theory & Questions
    Replies: 1
    Last Post: December 30th, 2011, 06:42 AM
  3. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  4. values of variables changing despite not modifying them
    By AndyKow in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 9th, 2011, 12:39 PM
  5. Changing Array variables from different classes
    By smellyhole85 in forum Collections and Generics
    Replies: 6
    Last Post: December 9th, 2010, 02:18 PM