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: How to fix this bug with my calculator program?

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    5
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default How to fix this bug with my calculator program?

    I'm developing a simple calculator program in java, with a gui, to help me learn java better, and i've fixed all the bugs with this program except one. Here is the code:
    import java.awt.BorderLayout;
     
    public class CalculatorProgram extends JFrame {
     
    	private JPanel contentPane;
    	private JTextField txtfield;
    	public double intone = 0;
    	public int lastoperator;
     
    	private JButton btnClear;
    	private JButton subtractBtn;
    	private JButton devideBtn;
    	private JButton multiplyBtn;
     
    	/**
    	 * Launch the application.
    	 */
    	public static void main(String[] args) {
    		EventQueue.invokeLater(new Runnable() {
    			public void run() {
    				try {
    					CalculatorProgram frame = new CalculatorProgram();
    					frame.setVisible(true);
    				} catch (Exception e) {
    					e.printStackTrace();
    				}
    			}
    		});
    	}
     
    	/**
    	 * Create the frame.
    	 */
    	public CalculatorProgram() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setBounds(100, 100, 450, 300);
    		contentPane = new JPanel();
    		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    		setContentPane(contentPane);
    		contentPane.setLayout(null);
     
    		// /////////////////////////////////////////////////////////////////////////////////////Textfield////////////////////////////////////////////////////////////////////////////////////////
    		txtfield = new JTextField();
    		txtfield.setFont(new Font("Tahoma", Font.BOLD, 10));
    		txtfield.setBounds(27, 11, 363, 30);
    		contentPane.add(txtfield);
    		txtfield.setColumns(10);
    		txtfield.addMouseListener(new MouseListener() {
     
    			@Override
    			public void mouseClicked(MouseEvent arg0) {
     
    			}
     
    			@Override
    			public void mouseEntered(MouseEvent arg0) {
     
    			}
     
    			@Override
    			public void mouseExited(MouseEvent arg0) {
     
    			}
     
    			@Override
    			public void mousePressed(MouseEvent arg0) {
     
    			}
     
    			@Override
    			public void mouseReleased(MouseEvent arg0) {
    				if (txtfield
    						.getText()
    						.equals("You need to set your number you want to calculate with!")
    						|| txtfield.getText().contains("Calculation is:")
    						|| !txtfield.getText().isEmpty()) {
    					txtfield.setText("");
    				}
     
    			}
     
    		});
    		// /////////////////////////////////////////////////////////////////////////////////////Text field////////////////////////////////////////////////////////////////////////////////////////
     
    		// //////////////////////////////////////////////////////////////////////////////////////Add Button/////////////////////////////////////////////////////////////////////////////////////////
    		JButton addBtn = new JButton("+");
    		addBtn.setFont(new Font("Tahoma", Font.BOLD, 17));
    		addBtn.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				String l = String.valueOf(intone);
    				lastoperator = 1;
    				if (intone == 0) {
    					try {
    						intone = Double.parseDouble(txtfield.getText());
    						System.out.println("Setting intone to " + intone);
    					} catch (Exception eeeeee) {
     
    					}
    				}else if(intone == Double.parseDouble(txtfield.getText())){
     
    				} else {
    					if(!txtfield.getText().equals(l)){
    					try {
    						intone = intone
    								+ Double.parseDouble(txtfield.getText());
    						System.out.println("Setting intone to " + intone);
    						txtfield.setText("" + intone);
    					} catch (Exception eeeeeeeeeee) {
     
    					}
     
     
    				}
    				}
    			}
    		});
    		addBtn.setBounds(37, 52, 50, 30);
    		contentPane.add(addBtn);
    		// //////////////////////////////////////////////////////////////////////////////////////Add Button/////////////////////////////////////////////////////////////////////////////////////////
     
    		// //////////////////////////////////////////////////////////////////////////////////////Equals Button/////////////////////////////////////////////////////////////////////////////////////
    		JButton equalsBtn = new JButton("=");
    		equalsBtn.setFont(new Font("Tahoma", Font.BOLD, 17));
    		equalsBtn.setBounds(178, 105, 50, 30);
    		contentPane.add(equalsBtn);
    		equalsBtn.addActionListener(new ActionListener() {
     
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				if (lastoperator == 1) {
    					try {
    						intone = intone
    								+ Double.parseDouble(txtfield.getText());
    						System.out.println("Setting intone to " + intone);
    						txtfield.setText("" + intone);
    						lastoperator = 0;
    					} catch (Exception abd) {
     
    					}
     
    				} else if (lastoperator == 2) {
    					try {
    						intone = intone
    								- Double.parseDouble(txtfield.getText());
    						System.out.println("Setting intone to " + intone);
    						txtfield.setText("" + intone);
    						lastoperator = 0;
    					} catch (Exception aot) {
     
    					}
     
    				} else if (lastoperator == 3) {
    					try {
    						intone = intone
    								/ Double.parseDouble(txtfield.getText());
    						System.out.println("Setting intone to " + intone);
    						txtfield.setText("" + intone);
    						lastoperator = 0;
    					} catch (Exception abbt) {
     
    					}
     
    				} else if (lastoperator == 4) {
    					try {
    						intone = intone
    								* Double.parseDouble(txtfield.getText());
    						System.out.println("Setting intone to " + intone);
    						txtfield.setText("" + intone);
    						lastoperator = 0;
    					} catch (Exception eeeee) {
     
    					}
     
    				}
     
    			}
     
    		});
    		// /////////////////////////////////////////////////////////////////////////////////////Equals button/////////////////////////////////////////////////////////////////////////////////////////
     
    		// /////////////////////////////////////////////////////////////////////////////////////Clear Button////////////////////////////////////////////////////////////////////////////////////////
    		btnClear = new JButton("Clear");
    		btnClear.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				System.out.println("Variables Cleared!");
    				intone = 0;
    				txtfield.setText("");
    			}
    		});
    		btnClear.setFont(new Font("Tahoma", Font.BOLD, 14));
    		btnClear.setBounds(172, 162, 70, 30);
    		contentPane.add(btnClear);
    		// /////////////////////////////////////////////////////////////////////////////////////Clear Button////////////////////////////////////////////////////////////////////////////////////////
     
    		// /////////////////////////////////////////////////////////////////////////////////////Subtract Button////////////////////////////////////////////////////////////////////////////////////////
    		subtractBtn = new JButton("-");
    		subtractBtn.setFont(new Font("Tahoma", Font.BOLD, 17));
    		subtractBtn.setBounds(124, 52, 50, 30);
    		contentPane.add(subtractBtn);
    		subtractBtn.addActionListener(new ActionListener() {
    			String ll = String.valueOf(intone);
    			@Override
    			public void actionPerformed(ActionEvent arg0) {
    				lastoperator = 2;
    				if (intone == 0) {
    					try {
     
    						intone = Double.parseDouble(txtfield.getText());
    						System.out.println("Setting intone to " + intone);
     
    					} catch (Exception a) {
     
    					}
    				}else if(intone == Double.parseDouble(txtfield.getText())){
     
    				} else {
     
    						try {
    							if(!txtfield.getText().equals(ll)){
    							intone = intone
    									- Double.parseDouble(txtfield.getText());
    							System.out.println("Setting intone to " + intone);
    							txtfield.setText("" + intone);
    							}
    						} catch (Exception eeeeeeeeeee) {
     
     
     
    				}
    			}
     
    		}});
    // /////////////////////////////////////////////////////////////////////////////////////Subtract Button////////////////////////////////////////////////////////////////////////////////////////
     
    	// /////////////////////////////////////////////////////////////////////////////////////Devide Button////////////////////////////////////////////////////////////////////////////////////////
    		devideBtn = new JButton("/");
    		devideBtn.setFont(new Font("Tahoma", Font.BOLD, 17));
    		devideBtn.setBounds(318, 52, 50, 30);
    		contentPane.add(devideBtn);
    		devideBtn.addActionListener(new ActionListener() {
    			String lll = String.valueOf(intone);
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				lastoperator = 3;
    				if (intone == 0) {
    					try {
    						intone = Double.parseDouble(txtfield.getText());
    						System.out.println("Setting intone to " + intone);
    					} catch (Exception p) {
     
    					}
    				}else if(intone == Double.parseDouble(txtfield.getText())){
     
    				} else {
     
    						try {
    							if(!txtfield.getText().equals(lll)){
    							intone = intone
    									/ Double.parseDouble(txtfield.getText());
    							System.out.println("Setting intone to " + intone);
    							txtfield.setText("" + intone);
    							}
    						} catch (Exception eeeeeeeeeee) {
     
    						}
     
     
     
    			}
     
    			}});
    		// /////////////////////////////////////////////////////////////////////////////////////Devide Button////////////////////////////////////////////////////////////////////////////////////////
     
    		// /////////////////////////////////////////////////////////////////////////////////////Multiply Button////////////////////////////////////////////////////////////////////////////////////////
     
    		multiplyBtn = new JButton("X");
    		multiplyBtn.setFont(new Font("Tahoma", Font.BOLD, 17));
    		multiplyBtn.setBounds(220, 52, 50, 30);
    		contentPane.add(multiplyBtn);
    		multiplyBtn.addActionListener(new ActionListener() {
    			String llll = String.valueOf(intone);
    			@Override
    			public void actionPerformed(ActionEvent e) {
    				lastoperator = 4;
    				if (intone == 0) {
    					try {
    						intone = Double.parseDouble(txtfield.getText());
    						System.out.println("Setting intone to " + intone);
    					} catch (Exception t) {
     
    					}
    				}else if(intone == Double.parseDouble(txtfield.getText())){
     
    				} else {
     
    						try {
    							if(!txtfield.getText().equals(llll)){
    							intone = intone
    									* Double.parseDouble(txtfield.getText());
    							System.out.println("Setting intone to " + intone);
    							txtfield.setText("" + intone);
    							}
    						} catch (Exception eeeeeeeeeee) {
     
    						}
     
     
     
    			}
     
    		}});
     
    		// /////////////////////////////////////////////////////////////////////////////////////Multiply Button////////////////////////////////////////////////////////////////////////////////////////
     
    	}
    }

    Now the problem is, let's say type in 4, then hit +, then hit 3. If you hit equals, it gives you the correct awnser, but if you dont hit equals, and decide to hit another operator button, it uses that operator on the button, instead of using the + you first typed in. So lets say you hit 3, then +, then 5, then -. Instead of printing out 8, it would subtract the 5 from 3 and give you that value. This is the only bug that has really troubled me.... And sorry for the probably really messy code. Anyone got any ideas on how to fix this?


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: How to fix this bug with my calculator program?

    One thing you should practice as an important aspect of being a better programmer in any language is to comment your code. You've posted a bunch of code without a single clue as to how it works. Effectively commented code is useful to someone who's never seen it before, and the comments can also be used to outline the logic of a program before and during the creative process of code writing. You'll be surprised how much the comments will help you remember what you've done or intended to do after you've put it aside for 2 or 3 days.

    Explaining the program's use of the variable 'intone' would be helpful, and I think if you try to explain it to yourself, writing comments as you go, you'll likely discover the source of your bug.

Similar Threads

  1. How to fix this class/program
    By profit7 in forum Collections and Generics
    Replies: 3
    Last Post: May 29th, 2013, 08:53 AM
  2. can anyone help me to fix the error in the program???
    By divi in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 25th, 2013, 01:36 AM
  3. can anyone help me to fix the error in the program???
    By divi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 24th, 2013, 02:49 PM
  4. How do I fix my program?
    By mjballa in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 15th, 2011, 01:00 AM
  5. can u guys help me fix my program
    By nima9006 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 14th, 2010, 03:43 AM