hi everyone, i have been asked to make a basic calculator but my calc only holds two integers like 2+2 it cant do more than 2 operations so id reaaly be glad if someone could take a look at my code and see what the problem is asap.im still a beginner in IT.thnx
this is what i have so far..............

/**
 * (#)calculator.java
 *  Real author : MIMI
 * version 1.00 2009/12/12
 */


	import java.awt.*;
	import javax.swing.*;
	import java.awt.event.*; 

	public class mycalculator extends JFrame implements ActionListener
		{
			JButton buttons[];
			JTextField text;
			JMenuBar men;
			JMenu file, help;
			JMenuItem ext, abt;
			JPanel pan1,pan2,pan3;
			String identifier;
			Boolean tester = false ;  
			Boolean holder = false ;
			Boolean	decimal = false;
			Double num1 , num2 , result;
			JButton clear ;



	 public  mycalculator()
  		{
  	 		men = new JMenuBar();
			setJMenuBar(men);
			ext = new JMenuItem("Exit");
			file = new JMenu("file");
			file.add(ext);
			men.add(file);
		
		
	
				
			abt = new JMenuItem("About");
			help = new JMenu("help");
			help.add(abt);
			men.add(help);
		
		
			ext.addActionListener(this);
			abt.addActionListener(this);
		
  	 		pan1 = new JPanel();
  	 		pan1.setLayout(new GridLayout(2,2,2,2));
			text = new JTextField("");
			text.setHorizontalAlignment(JTextField.RIGHT);
			pan1.add(text);
  	
  			pan2 = new JPanel();
  			pan2.setLayout(new GridLayout(4,4,4,4));
  			buttons = new JButton[16];

  	
  	
  			String str[] = {"7","8","9","*","4","5","6","/","1","2","3","+",".","0","=","-"};
  			for(int i=0 ; i<buttons.length ; i++)
  			{
  			buttons[i] = new JButton(str[i]);
  			pan2.add(buttons[i]);
  			buttons[i].addActionListener(this);
  			pan3 = new JPanel();
  			clear = new JButton("clear");
  			pan3.add(clear);
  			clear.addActionListener(this);
  		
  			  		
  			setLayout(new BorderLayout());
  			add(pan1 ,BorderLayout.NORTH);
  			add(pan2 ,BorderLayout.CENTER);
  			add(pan3 ,BorderLayout.SOUTH);
 			setVisible(true);
  			setSize(350,300);
  			setResizable(false);
  			setDefaultCloseOperation(EXIT_ON_CLOSE);
  			setTitle("My calcualtor");
  		
  			}
  	
  			}
  			public void actionPerformed(ActionEvent evt)
  				
  			{
  	   		
  	   		if(evt.getSource() == abt)
			{
				JOptionPane.showMessageDialog(null, "author : ");	
			}
			
		 	else if(evt.getSource() == ext)
			{	
		 		System.exit(0);	
			}	
  	   		
  	   		
			
  	   		if(evt.getActionCommand()== "1")
  	   		{if (text.getText().equals("0"))
  	   	       	 text.setText("1");
  	   	       
  	   	     else if (tester == true)
  	   	     {
  	   	       	text.setText("1");
  	   	       	tester = false;
  	   	     }
  	   	       else
  	   	       text.setText(text.getText() + "1");
  	   
  	   		}
  	   		else if(evt.getActionCommand()== "2")
  	   		{
  	   		if (text.getText().equals("0"))
  	           text.setText("2");
  	            
  	        else if (tester == true)
  	        {
  	            	text.setText("2");
  	            	tester = false;
  	        }
  	            else
  	          	text.setText(text.getText() + "2");
  	   		}
  	   		
  	   		else if(evt.getActionCommand()== "3")
  	   	    {	
  	   	    if (text.getText().equals("0"))
  	   	   	    text.setText("3");
  	   	   	           
  	   	   	else if (tester == true )
  	   	   	 {
  	   	   	    text.setText("3");
  	   	   	    tester = false;
  	   	   	 }
  	   	   	 else
  	   	   	 text.setText(text.getText()+ "3");
  	   		}
  	   		else if(evt.getActionCommand()== "4")
  	   		{   
  	   		if (text.getText().equals("0"))
  	   		    text.setText("4");
  	   		       
  	   		else if (tester == true )
  	   	   	{
  	   	   	     text.setText("4");
  	   	   	     tester = false;
  	   	   	}
  	   	   	else
  	   	   	text.setText(text.getText()+ "4");
  	   		}
  	   		else if(evt.getActionCommand()== "5")
  	   		{  
  	   		if(text.getText().equals("0"))
  	   		   text.setText("5");
  	   		      
  	   		else if (tester == true )
  	   	   	 {
  	   	   	           	text.setText("5");
  	   	   	           	tester = false;
  	   	   	 }
  	   	   	else
  	   	   	text.setText(text.getText()+ "5");
  	   		}
  	   		else if(evt.getActionCommand()== "6")
  	   		{if(text.getText().equals("0"))
  	   		text.setText("6");
  	   		      
  	   		else if (tester == true )
  	   	   	{
  	   	   	 text.setText("6");
  	   	   	 tester = false;
  	   	   	}
  	   	   	  else
  	   	   	 text.setText(text.getText()+ "6");
  	   		}
  	   		else if(evt.getActionCommand()== "7")
  	   		{   if(text.getText().equals("0"))
  	   			text.setText("7");
  	   		
  	   		 else if (tester == true )
  	   	   	 {
  	   	  		text.setText("7");
  	   	   	    tester = false;
  	   	   	 }
  	   	   	    else
  	   	 		text.setText(text.getText()+ "7");
  	   		}
  	   		else if(evt.getActionCommand()== "8")
  	   		{   if(text.getText().equals("0"))
				text.setText("8");
  	   		      
  	   		else if (tester == true )
  	   	   	{
  	   	   	 text.setText("8");
  	   	   	 tester = false;
  	   	   	}
  	   	   	else
  	   	   	text.setText(text.getText()+ "8");
  	   		}
  	   		else if(evt.getActionCommand()== "9")
  	   		{ if(text.getText().equals("0"))
  	   		text.setText("9");
  	   		
  	   		else if (tester == true )
  	   	   	{
  	   	   	text.setText("9");
  	   	   	tester = false;
  	   	   	}
  	   	   	 else
  	  		text.setText(text.getText()+ "9");
  	   		}
  	   		else if(evt.getActionCommand()== "0")
  	   		{  if (text.getText().equals("0"))
  	   	    text.setText("0");
  	   	      
  	   	   	else if (tester == true )
  	   	   	{
  	   	   	 text.setText("0");
  	   	   	 tester = false;
  	   	   	 }
  	   	   	 else
  	   	   	 text.setText(text.getText()+ "0");
  	   	 	}
  	   	 	else if (evt.getActionCommand() == "clear")
			{
				text.setText(null);
  	            	tester = false;		
			}
  	   	 	else if (evt.getActionCommand() == ".")
  	   	 	{if(decimal == false)
  	   	 	{
  	   	 	text.setText(text.getText()+ ".");
  	   	 	decimal = true;
  	   	 	}
  	     	}  
  	   		else if (evt.getActionCommand() == "+")
  	   		{
  	   		num1 = Double.parseDouble(text.getText());
  	   		tester = true;
  	   		identifier = "+";
  	   		decimal = false;
  	   		holder = true;
  	   		   
  	   		}
  	   		else if (evt.getActionCommand() == "-")
  	   		{
  	   		num1 = Double.parseDouble(text.getText());
  	   		tester = true;
  	   		identifier = "-";
  	   		decimal = false;
  	   		holder = true;
  	   		       
  	   		}
  	   		else if (evt.getActionCommand() == "*")
  	   		{
  	   		num1 = Double.parseDouble(text.getText());
  	   		tester = true;
  	   		identifier = "*";
  	   		decimal = false;
  	   		holder = true;
  	   		        
  	   		}
  	   		else if (evt.getActionCommand() == "/")
  	   		{
  	   		num1 = Double.parseDouble(text.getText());
  	   		tester = true;
  	   		identifier = "/";
  	   		decimal = false;
  	   		holder = true;
  	   		       
  	   		}
  	   		else if (evt.getActionCommand() == "=")
  	   		{
  	   		num2 = Double.parseDouble(text.getText());
  	   		tester = true;
  	   		holder = false;
  	   		if (identifier == "+")
  	   	  	{
  	   		    result = num1 + num2;
  	   			text.setText(String.valueOf(result));
  	   			
  	   		}
  	   		else if (identifier =="-")
  	   	   {
  	   			result = num1 - num2;
  	   			text.setText(String.valueOf(result));
  	   		}
  	   		else if (identifier == "*")
  	   		{
  	   	            result = num1 * num2;
  	   	            text.setText(String.valueOf(result));
  	   		}
  	   		else if (identifier == "/")
  	   		{
  	   			result = num1 / num2;
  	   			text.setText(String.valueOf(result));
  	   		}
  	   			
  	   			
  	   		}
			}
  
  	public static void main (String[] args) 
  	{
  		new mycalculator();
	
  	}}

It can only do one operation (not two), addition is only one operation . . the reason it can only do one operation is because once the equal sign is clicked, you only scan through num1 and num2 and take an appropriate operation on those variables. In order to allow your calculator to take unlimited operations, when a button is clicked, you should add that button's text to an array. For example, if 2 is clicked, then +, then 3, your array would hold

Index | What it Holds
index 0 holds 2
index 1 holds +
index 2 holds 3

Now lets say + 4 + 5 is clicked, then it would hold

index 0 holds 2
index 1 holds +
index 2 holds 3
index 3 holds +
index 4 holds 4
index 5 holds +
index 6 holds 5

There is another way to do it where your calculator would constantly update the value it is displaying; for example, if you clicked 2 + 3 it would say 5, then if you clicked * 4 it would update to 20. This way is essentially exactly the same, except you're assuming that you've stored a "result" which computes up to a certain point in the array, then when the user keeps clicking more buttons, you add those to the array also, and you can (instead of recomputing the entire array), start from where you left off and add onto result. So I'd recommend making the following methods:

1. A method that uses two variables; a result and the currentIndex where the result computes up to that point. For example, if you haven't calculated anything yet, result would be 0 and the currentIndex would be 0 (which is the start of the array). This method would, using the previous result and the currentIndex, compute the new results.

2. A method that checks to make sure the input was valid. I.e. your program will not work correctly if the user enters "1 + + 2" or any series of symbols that doesn't make sense.

Then call those methods from within actionPerformed

well i get the idea but im still having problems!i don't know what to do??i looked at it so much im having a headache loool!welll im more the practical type than theoritical. anyways thnx.but id still be really grateful if when u r free, u could find an easier way to show me how code works practically.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.