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 2 of 2

Thread: Odd Instance Field Issue

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Odd Instance Field Issue

    My issue is that eclipse is telling me that the decimal instance field is never read locally, despite the fact that it's an instance field meaning it should be a class variable. Therefore, when I compile the code it assumes that decimal is true by default, as I never set it to false. Checkpoint print statements inside the if (decimal = false) statement never print in the console, meaning that large chunk of code for digits 1-9 is never even accessed. But statements inside the if decimal = true statement do print, meaning java does access that statement. I only set decimal to true inside the if decimal = false statement, which isn't accessed meaning the only way for the if = true code to be accessed is that decimal is being read as true by default.

    I honestly have no idea what's wrong, as decimal IS read locally multiple times, with the correct syntax...

    Any help would be appreciated. Thank you!!!

    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import javax.swing.*;
     
    public class Project7 extends JFrame {
    	private JButton[] buttons = new JButton[10];
    	private JButton buttonDot = new JButton(".");
    	private JButton clear = new JButton("clear");
    	private boolean decimal = false;
    	private JLabel label1 = new JLabel("0.0");
    	private JPanel innerPanel = new JPanel();
     
    	private JPanel outerPanel = new JPanel();
     
     
     
    	public Project7() { 
    		// this refers to "this" MyFirstGui object that is executing this constructor.
    		this.setLayout(null); // no layout manager. I will layout the components myself
    		this.setSize(600,700);
    		this.setLocation(100,40);
    		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		this.setTitle("This is my Numeric Pad");
     
    		Font font = new Font("Arial", Font.BOLD, 30);
     
    		ButtonListener ears = new ButtonListener();
     
    		for(int i=1; i<10; i++){
    			innerPanel.add(buttons[i] = new JButton(""+i));
    			// put more code in here that sets up the buttons and adds them to a container (innerPanel)
    		}
     
     
     
    		buttons[0] = new JButton("0");
    		buttons[0].setVisible(true);
    		// now you have 10 buttons in your array
     
    		BorderLayout manage = new BorderLayout();
    		outerPanel.setLayout(manage);
    		outerPanel.setSize(350,200);
    		outerPanel.setLocation(10,20);
    		outerPanel.setBackground(Color.WHITE);
    		label1.setFont(font);
    		label1.setBackground(Color.WHITE);
    		outerPanel.add(label1, BorderLayout.NORTH);
     
     
    		GridLayout manage3 = new GridLayout(4,3);
    		innerPanel.setLayout(manage3);
    		innerPanel.add(buttons[0]);
    		innerPanel.add(buttonDot);
    		innerPanel.add(clear);
    		innerPanel.setBackground(Color.WHITE);
    		outerPanel.add(innerPanel);
     
    		buttons[0].addActionListener(ears);
    		buttons[1].addActionListener(ears);
    		buttons[2].addActionListener(ears);
    		buttons[3].addActionListener(ears);
    		buttons[4].addActionListener(ears);
    		buttons[5].addActionListener(ears);
    		buttons[6].addActionListener(ears);
    		buttons[7].addActionListener(ears);
    		buttons[8].addActionListener(ears);
    		buttons[9].addActionListener(ears);
    		buttonDot.addActionListener(ears);
    		clear.addActionListener(ears);
     
     
    		/*
    		 * I set the outerPanel to have a BorderLayout. I added the label1 to BorderLayout.NORTH.
    		 * I added the inner panel to BorderLayout.CENTER
    		 * 
    		 * My inner panel is using GridLayout. You must use GridLayout for innerPanel: 4 rows, 3 col.
    		 */
    		outerPanel.setSize(300, 500);
    		outerPanel.setBackground(Color.WHITE);
    		outerPanel.setLocation(100, 10);
    		outerPanel.setVisible(true);
    		this.add(outerPanel);
     
     
     
     
    	} // end Project7 constructor
     
     
    	/* inner class we will use to instantiate a listener for a button
    	 * If you want to have 12 different inner classes (1 per button) go ahead.
    	 * Or you can register one listener to take care of all of them. You can distinguish
    	 * who was clicked with the code I have given you below.
    	 */
    	public class ButtonListener implements ActionListener { 
    		public void actionPerformed(ActionEvent arg0) {
    			JButton clickedOn = (JButton)arg0.getSource();
    			String initial = label1.getText();
     
    			if (clickedOn==clear) {
    				label1.setText("0.0");
    				d = false;
    			}
     
    			if(decimal = false) {
    				System.out.println("y");
    			if(clickedOn==buttons[0]) {
     
    				String current0 = label1.getText();
    				label1.setText("0" + current0 );
    			}
     
    			else if (clickedOn==buttons[1]){
    				String current1 = label1.getText();
    				label1.setText("1" +current1);
    			}
    			else if (clickedOn==buttons[2]){
    				String current2 = label1.getText();
    				label1.setText("2" + current2);
    			}
    			else if (clickedOn==buttons[3]){
    				String current3 = label1.getText();
    				label1.setText("3" + current3);
    			}
    			else if (clickedOn==buttons[4]){
    				String current4 = label1.getText();
    				label1.setText("4" + current4);
    			}
    			else if (clickedOn==buttons[5]){
    				String current5 = label1.getText();
    				label1.setText("5" + current5);
    			}
    			else if (clickedOn==buttons[6]){
    				String current6 = label1.getText();
    				label1.setText("6" + current6);
    			}
    			else if (clickedOn==buttons[7]){
    				String current7 = label1.getText();
    				label1.setText("7" + current7);
    			}
    			else if (clickedOn==buttons[8]){
    				String current8 = label1.getText();
    				label1.setText("8" + current8);
    			}
    			else if (clickedOn==buttons[9]){
    				String current9 = label1.getText();
    				label1.setText("9" + current9);
    			}
    			else if (clickedOn==buttonDot) {
    				String currentd = label1.getText();
    				int x = currentd.length();
    				String s = currentd.substring(0, x-2);
    				label1.setText(s + ".");
    				decimal = true;
    			}
    			}
     
     
    			if (decimal = true) {
    				System.out.println("x");
    				String initiald = label1.getText();
    				if(clickedOn==buttons[1]) {
    					label1.setText(initiald + "1");
    				}
    			}
    			/* Refer to clickedOn when you want to do something with the most recently clicked button.
    			   Such as, you will want to ask clickedOn for the text that is written on it.
    			   clickedOn.getText();
     
    			   You can get the text in label1 the same way: label1.getText()
    			   You can set the text in label1: label1.setText("the string you want to display");
    			*/
    		}
    	}// end of ButtonListener class
     
     
     
     
    }// end of Project7 class


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Odd Instance Field Issue

    if(decimal = false)
    You've fallen for the classic mistake between = and ==. = is the assignment operator, == is the equality comparison operator.

    What that code is doing is assigning false to decimal, then evaluating if(false), which results in nothing being printed out.

Similar Threads

  1. Replies: 3
    Last Post: February 21st, 2011, 07:25 PM
  2. Using a password field
    By javapenguin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 10th, 2010, 11:54 AM
  3. [SOLVED] how to get each field of a current date
    By chronoz13 in forum Java Theory & Questions
    Replies: 4
    Last Post: January 24th, 2010, 06:33 AM
  4. Creating new instance
    By vluong in forum Object Oriented Programming
    Replies: 2
    Last Post: November 28th, 2009, 11:35 PM
  5. [SOLVED] find the position of the field separator in the String---need help ASAP
    By rajesh.mv in forum Java Theory & Questions
    Replies: 6
    Last Post: August 17th, 2009, 10:33 AM