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