My operators are pretty much working well, but the problem is every time I do an operation (example: 1+2 = 3) and then tried clicking another number the number that I clicked adds up with the result (so this is what happens, the result is 3, then I click 7 the text field will contain 3.07). It should be when I press 7 the text field should contain 7 not 3.07. Please help me! Thanks!

BTW: I haven't worked with the other buttons since I wanted to finish this one off befre proceeding.

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

public class ScientificCalculator
{
	double num1 = 0;
	double num2 = 0;
	double num3 = 0;
	double result = 0;
	String sign = "";
	public JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, bAdd, bSub, bDiv, bMul, bClear, bEquals, bFrac,
			bMC, bMR, bMS, bMPos, bCbRt, bDec, bPerc, bSqrt, bPosNeg, bCE, bBackSpace, bMod, bAnd, bOR, bXor, bNot, bInt, bSquare, bPi,
			bA, bB, bC, bD, bE, bF;
	public JTextField tf1;
	
	public JFrame f;
	public JPanel p1, p2, p3, p4, p5, p6, p7, p8;
	public JRadioButton rb1, rb2, rb3;
	public ButtonGroup group;
	boolean secondEqual, secondNum;
	
	public ScientificCalculator()
	{
		f = new JFrame("Calculator");
		
		p1 = new JPanel();
		p2 = new JPanel(new GridLayout(1,3, 5,5));
		p3 = new JPanel(new GridLayout(1,7, 5,5));
		p4 = new JPanel(new GridLayout(1,7, 5,5));
		p5 = new JPanel(new GridLayout(1,7, 5,5));
		p6 = new JPanel(new GridLayout(1,7, 5,5));
		p7 = new JPanel(new GridLayout(1,7, 5,5));
		p8 = new JPanel(new GridLayout(1,7, 5,5));
		
		tf1 = new JTextField("", 20);
		tf1.setHorizontalAlignment(JTextField.TRAILING);
		tf1.setEditable(false);
		
		rb1 = new JRadioButton("Hex");
		rb2 = new JRadioButton("Octal");
		rb3 = new JRadioButton("Decimal");
		
		b1 = new JButton("1");
		b2 = new JButton("2");
		b3 = new JButton("3");
		b4 = new JButton("4");
		b5 = new JButton("5");
		b6 = new JButton("6");
		b7 = new JButton("7");
		b8 = new JButton("8");
		b9 = new JButton("9");
		b10 = new JButton("0");
		
		bAdd = new JButton("+");
		bSub = new JButton("-");
		bMul = new JButton("*");
		bDiv = new JButton("/");
		bClear = new JButton("C");
		bEquals = new JButton("=");
		bFrac = new JButton("1/x");
		bMC = new JButton("MC");
		bMR = new JButton("MR");
		bMS = new JButton("MS");
		bDec = new JButton(".");
		bPerc = new JButton("%");
		bSqrt = new JButton("srt");
		bMPos = new JButton("M+");
		bCbRt = new JButton("M-");
		bPosNeg = new JButton("±");
		bCE = new JButton("CE");
		bBackSpace = new JButton("<--");
		bMod = new JButton("Mod");
		bAnd = new JButton("And");
		bOR = new JButton("Or");
		bXor = new JButton("Xor");
		bPi = new JButton("Pi");
		bNot = new JButton("Not");
		bInt = new JButton("Int");
		bSquare = new JButton("X2");
		bA = new JButton("A");
		bB = new JButton("B");
		bC = new JButton("C");
		bD = new JButton("D");
		bE = new JButton("E");
		bF = new JButton("F");
	
		group = new ButtonGroup();
		group.add(rb1);
		group.add(rb2);
		group.add(rb3);
		
	}
	public void doStuff()
	{
		p1.add(tf1);
		
		p2.add(rb1);
		p2.add(rb2);
		p2.add(rb3);
		
		p3.add(bClear);
		p3.add(bCE);
		p3.add(bBackSpace);
		p3.add(bMod);
		p3.add(bAnd);
		p3.add(bOR);
		p3.add(bA);
		
		p4.add(bMC);
		p4.add(b7);
		p4.add(b8);
		p4.add(b9);
		p4.add(bXor);
		p4.add(bNot);
		p4.add(bB);
		
		p5.add(bMR);
		p5.add(b4);
		p5.add(b5);
		p5.add(b6);
		p5.add(bAdd);
		p5.add(bInt);
		p5.add(bC);
		
		p6.add(bMS);
		p6.add(b1);
		p6.add(b2);
		p6.add(b3);
		p6.add(bDiv);
		p6.add(bSqrt);
		p6.add(bD);
	
		p7.add(bMPos);
		p7.add(b10);
		p7.add(bDec);
		p7.add(bEquals);
		p7.add(bSub);
		p7.add(bPi);
		p7.add(bE);
	
		p8.add(bCbRt);
		p8.add(bPerc);
		p8.add(bFrac);
		p8.add(bPosNeg);
		p8.add(bMul);
		p8.add(bSquare);
		p8.add(bF);
				
		f.setLayout(new GridLayout(8,6, 5,5));
		
		b1.addActionListener(new numberHandler());
		b2.addActionListener(new numberHandler());
		b3.addActionListener(new numberHandler());
		b4.addActionListener(new numberHandler());
		b5.addActionListener(new numberHandler());
		b6.addActionListener(new numberHandler());
		b7.addActionListener(new numberHandler());
		b8.addActionListener(new numberHandler());
		b9.addActionListener(new numberHandler());
		b10.addActionListener(new numberHandler());
		bAdd.addActionListener(new numberHandler());
		bSub.addActionListener(new numberHandler());
		bMul.addActionListener(new numberHandler());
		bDiv.addActionListener(new numberHandler());
		bEquals.addActionListener(new numberHandler());
		bCE.addActionListener(new numberHandler());
		
		f.add(p1);
		f.add(p2);
		f.add(p3);
		f.add(p4);
		f.add(p5);
		f.add(p6);
		f.add(p7);
		f.add(p8);
		f.setSize(450,240);
		f.setResizable(false);
		f.setVisible(true);
	}
	public static void main(String args[])
	{
		ScientificCalculator c = new ScientificCalculator();
		c.doStuff();
	}
public class numberHandler implements ActionListener
{
  public void actionPerformed(ActionEvent e) {
		if(e.getSource() == b1)
			{
				tf1.setText(tf1.getText() + "1");
			}
		else if(e.getSource() == b2)
			{
				tf1.setText(tf1.getText() + "2");
			}
		else if(e.getSource() == b3)
			{
				tf1.setText(tf1.getText() + "3");
			}
		else if(e.getSource() == b4)
			{
				tf1.setText(tf1.getText() + "4");
			}
		else if(e.getSource() == b5)
			{
				tf1.setText(tf1.getText() + "5");
			}
		else if(e.getSource() == b6)
			{
				tf1.setText(tf1.getText() + "6");
			}
		else if(e.getSource() == b7)
			{
				tf1.setText(tf1.getText() + "7");
			}
		else if(e.getSource() == b8)
			{
				tf1.setText(tf1.getText() + "8");
			}
		else if(e.getSource() == b9)
			{
				tf1.setText(tf1.getText() + "9");
			}
		else if(e.getSource() == b10)
			{
				tf1.setText(tf1.getText() + "0");
			}
		else if(e.getSource() == bAdd)
			{
				Calculate("+");
				tf1.setText("");
			}
		else if(e.getSource() == bSub)
			{
				num1 = Double.parseDouble(tf1.getText());
				Calculate("-");
				tf1.setText("");
			}
		else if(e.getSource() == bMul)
			{
				num1 = Double.parseDouble(tf1.getText());
				Calculate("*");
				tf1.setText("");
			}
		else if(e.getSource() == bDiv)
			{
				num1 = Double.parseDouble(tf1.getText());
				Calculate("/");
				tf1.setText("");
			}
		else if(e.getSource() == bEquals)
			{
				
				Calculate("=");
			}
		else if(e.getSource() == bCE)
			{
				tf1.setText("0");
			}
  }
public void Calculate(String operator)	
	{
		if(operator.equals("="))
		{
				if(secondEqual)	{	
					num1 = result;			
					result = Process();		
					tf1.setText(new Double(result).toString());			
				}	
				else 
				{				
					num2 = Double.parseDouble(tf1.getText());														
					result = Process();												
					tf1.setText(new Double(result).toString());	
						if(operator != "+" && operator != "-" && operator != "*" && operator != "/" && operator != "=" )
						{
							tf1.setText("");
						}
					num1 = result;	
										
					secondEqual = true;								
					secondNum = false;
				}	
	        }
	
			else {
				if(secondNum)
				{	
					num2 = Double.parseDouble(tf1.getText());					
					result = Process();				
					tf1.setText(new Double(result).toString());					
					num1 = result;	
					secondNum = false;						
				}	
				else 
				{
					num1 = Double.parseDouble(tf1.getText());	
					tf1.setText("");					
				}														
				sign = operator;	
				secondEqual = false;
			}
		
	}
	public double Process()	{
		if(sign.equals("*"))
			return (num1 * num2);
		else if(sign.equals("-"))
			return (num1 - num2);
		else if(sign.equals("+"))
			return (num1 + num2);
		else if(sign.equals("/"))	{
				return (num1 / num2);
		}	
		return 0;
	}
}

}

Recommended Answers

All 4 Replies

you propably forgot to reset a value you use to perform the calculation.

I can't locate it though I tried changing the numberHandler part.

Do you need a state flag that tells you where you are in the get input / show results cycle?
Use the flag to clear out the current values shown and display the newly entered ones.

As stultuske said, you need to reset the value of result. Just add after displaying result and option to clear the result.

You can use StringBuffer for result type. Then you can add result like this:

StringBuffer result = new StringBuffer("");

result.replace(0, result.Lenght(), "new result");

And when you call it to display:

result.toString();
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.