Results 1 to 4 of 4
  1. #1
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Clear button on GUI calculator not working correctly

    I would like my clear button to set all variables to 0 and allow me to continue making calculations, but instead it (and I dont know why) once it clears everything the results of my next operations (add, sub, etc...) are wrong. Also the clear button will only work if I enter a digit before pressing clear. So I cant do 2+2 = then press clear, instead I must do 2 + 2 =, then lets say 5 clear to get clear to call. Any help is appreciated.

    Java Code:
     public void calculate(double x, String lastCommand, JButton jDisplay)
    	{		
    		if (lastCommand.equals("+")){ 
    			result += x;
    			jDisplay.setText("" + result);
    		}
    		else if (lastCommand.equals("-")) {
    			result = Math.abs(result - x);
    			jDisplay.setText("" + result);
    		}
    		else if (lastCommand.equals("*")){
    			
    			if (result == 0){
    				ans=( result *x) + x;
    			
    				jDisplay.setText("" + ans);
    				result++;
    			}
    			else {	
    				ans *= x;
    				jDisplay.setText("" + ans);
    			}
    			
    			result++;
    		}
    		
    		else if (lastCommand.equals("/")){
    			
    			if (result == 0){
    				ans=( result /x) + x;
    			
    				jDisplay.setText("" + ans);
    				result++;
    			} 
    			else {
    				ans /= x;
    				jDisplay.setText("" + ans);
    			}
    			
    			result++;
    		}
    		
    		else if (lastCommand.equals("=")){
    			
    			ans=x;
    			jDisplay.setText("" + ans);
    			
    		}
    		
    		else if (lastCommand.equals("C")){
    			x=0;
    			result = 0;
    			ans=0;
    			jDisplay.setText("" + result);
    		}
    
    	}
    Java Code:
    public class Runner  extends JPanel implements ActionListener {
    
    	ComplexOperations complex = new ComplexOperations();
    
    	public Runner()
    	{
    		setLayout(new BorderLayout());
    
    	      lastCommand = "=";
    	      run = true;
    	      }
    	
    
    	public void actionPerformed(ActionEvent event) {
    
    		String input = event.getActionCommand();
    		if(input.equals("0") || input.equals("1") || input.equals("2") || ...)
    		{
    			if (complex.run) 
    			{
    				jDisplay.setText("");
    				complex.run = false;
    			}
    			jDisplay.setText(jDisplay.getText() + input);
    		}
    		else
    		{
    		
    			String command = event.getActionCommand();
    
    			if (complex.run)
    			{  
    					lastCommand = command;	
    			}
    			else
    			{ 
    				lastCommand = command;
    				complex.calculate(Double.parseDouble(jDisplay.getText()), lastCommand, jDisplay);
    				lastCommand = command;
    				complex.run = true;
    		}
    
    	     }
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Clear button on GUI calculator not working correctly

    How can the code be compiled and executed for testing?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    VettesRus is offline Member
    Join Date
    Jun 2012
    Posts
    35
    Rep Power
    0

    Default Re: Clear button on GUI calculator not working correctly

    Should I upload a zip with all my source files?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Clear button on GUI calculator not working correctly

    If there are more than 300 lines of code, you need to make a small, complete program that compiles, executes and shows the problem. If there are fewer lines, post the code in the thread. Don't post a zip.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Clear button doesn't work
    By BobbyH in forum AWT / Swing
    Replies: 1
    Last Post: 10-09-2013, 12:45 AM
  2. Help with If/Else not working correctly?
    By kalaiss in forum New To Java
    Replies: 8
    Last Post: 04-16-2012, 06:32 AM
  3. Solving Clear Button
    By Ryan10 in forum New To Java
    Replies: 73
    Last Post: 04-13-2011, 05:04 AM
  4. Adding a sqrt button and percentage button to a calculator
    By Josie_Taylor in forum New To Java
    Replies: 4
    Last Post: 03-14-2011, 01:16 AM
  5. Clear Radio Button
    By Reborn in forum New To Java
    Replies: 6
    Last Post: 07-25-2010, 05:21 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
  •