Link to home
Start Free TrialLog in
Avatar of perdoname_
perdoname_

asked on

Calculator

Hello Experts,
Can someone give a look to that code ??
It was supposed to be a swing calculator but i cant figure out what's going wrong...

If anyone can help me it'd be grateful!

Thanks in advance!

import java.awt.*;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Container;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.*;

public class Calculator extends JFrame implements ActionListener
{
    public static final int Width = 500;
    public static final int Height = 500;
   
    private JTextField Board;
    private JButton jbo0, jbo1, jbo2, jbo3, jbo4, jbo5, jbo6, jbo7, jbo8, jbo9,
                    jboAddition, jboSubtraction, jboMultiplication, jboDivision,
                    jboDot, jboLp, jboRp, jboClear, jboResult;
                   
   /*                
    public static void main(String args[])
  {
    JFrame applet = new Calculator();
    JFrame frame = new JFrame();
    frame.add(frame);
    frame.setSize(Width,Height);
    frame.show();
  }
  */
 
 public static void main(String args[])
  {
    JFrame outputFrame = new Calculator();
   

//panel1.add(allyourstuff);
//panel1.add(moreofyourstuff);

    outputFrame.setVisible(true);
  }
    public Calculator()
    {
        Container outputPane = this.getContentPane();
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(Width, Height);
        outputPane.setLayout(new GridLayout (3, 3));
       
        Panel panel1 = new Panel();
        this.setDefaultCloseOperation( EXIT_ON_CLOSE );
        Board = new JTextField();
        panel1.add(Board);
        outputPane.add(panel1);

        //panel1.add(jbo0);
 
        //Numbers
        setLayout(new FlowLayout());
        setFont(new Font("Helvetica", Font.PLAIN, 8));

        JButton jbo0 = new JButton("0");
        jbo0.addActionListener(this);
        panel1.add(jbo0);
        jbo1 = new JButton("1");
        panel1.add(jbo1);
        jbo1.addActionListener(this);
        jbo2 = new JButton("2");
        panel1.add(jbo2);
        jbo2.addActionListener(this);
        jbo3 = new JButton("3");
        panel1.add(jbo3);
        jbo3.addActionListener(this);
        jbo4 = new JButton("4");
        panel1.add(jbo4);
        jbo4.addActionListener(this);
        jbo5 = new JButton("5");
        panel1.add(jbo5);
        jbo5.addActionListener(this);
        jbo6 = new JButton("6");
        panel1.add(jbo6);
        jbo6.addActionListener(this);
        jbo7 = new JButton("7");
        panel1.add(jbo7);
        jbo7.addActionListener(this);
        jbo8 = new JButton("8");
        panel1.add(jbo8);
        jbo8.addActionListener(this);
        jbo9 = new JButton("9");
        panel1.add(jbo9);
        jbo9.addActionListener(this);
       
        //Math Operations
       
        jboAddition = new JButton("+");
        panel1.add(jboAddition);
        jboAddition.addActionListener(this);
        jboSubtraction = new JButton("-");
        panel1.add(jboSubtraction);
        jboSubtraction.addActionListener(this);
        jboMultiplication = new JButton("*");
        panel1.add(jboMultiplication);
        jboMultiplication.addActionListener(this);
        jboDivision = new JButton("/");
        panel1.add(jboDivision);
        jboDivision.addActionListener(this);
       
        //Result etc..
       
        jboDot = new JButton(".");
        panel1.add(jboDot);
        jboDot.addActionListener(this);
        jboLp = new JButton("(");
        panel1.add(jboLp);
        jboLp.addActionListener(this);
        jboRp = new JButton(")");
        panel1.add(jboRp);
        jboRp.addActionListener(this);
        jboClear = new JButton("C");
        panel1.add(jboClear);
        jboClear.addActionListener(this);
        jboResult = new JButton("=");
        panel1.add(jboResult);
        jboResult.addActionListener(this);
       


    }
   
    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() instanceof JButton)
        {
            JButton buClicked = (JButton) e.getSource();
            if (buClicked == jboClear)
            {
                boardClear();
            }
            else if(buClicked == jboResult)
            {
                Calculate();
            }
        }
        else
        {
            Calculate();
        }
    }
   
    public void UserInput(JButton buClicked)
    {
        String input;
        input = Board.getText();
        if (buClicked == jbo0)
        {
            Board.setText(input + "0");
        }
        if (buClicked == jbo1)
        {
            Board.setText(input + "1");
        }
        if (buClicked == jbo2)
        {
            Board.setText(input + "2");
        }
        if (buClicked == jbo3)
        {
            Board.setText(input + "3");
        }
        if (buClicked == jbo4)
        {
            Board.setText(input + "4");
        }
        if (buClicked == jbo5)
        {
            Board.setText(input + "5");
        }
        if (buClicked == jbo6)
        {
            Board.setText(input + "6");
        }
        if (buClicked == jbo7)
        {
            Board.setText(input + "7");
        }
        if (buClicked == jbo8)
        {
            Board.setText(input + "8");
        }
        if (buClicked == jbo9)
        {
            Board.setText(input + "9");
        }
        if (buClicked == jboAddition)
        {
            Board.setText(input + "+");
        }
        if (buClicked == jboSubtraction)
        {
            Board.setText(input + "-");
        }
        if (buClicked == jboMultiplication)
        {
            Board.setText(input + "*");
        }
        if (buClicked == jboDivision)
        {
            Board.setText(input + "/");
        }
        if (buClicked == jboDot)
        {
            Board.setText(input + ".");
        }
        if (buClicked == jboLp)
        {
            Board.setText(input + "(");
        }
        if (buClicked == jboRp)
        {
            Board.setText(input + ")");
        }
    }
   
     private void boardClear()
    {
        Board.setText("");
    }
   
    public void Calculate()
    {
        int counter;
        int numParenthesis = 0;
        int lenInput;
        String calc;
        String Answer = "";
        char NumOther;
       
        calc = Board.getText();
        lenInput = calc.length();
       
        for (counter = 0; counter < lenInput; counter++)
        {
            NumOther = calc.charAt(counter);
            if (NumOther == ')')
            {
                numParenthesis--;
            }
            if (NumOther == '(')
            {
                numParenthesis++;
            }
            if ((NumOther < '(') || (NumOther > '9') || (NumOther == '.'))
            {
                Board.setText("Error");
            }
            if (NumOther == '.' && (counter + 1 < calc.length()))
            {
                for (int k = counter + 1; (k < calc.length()) && ((Character.isDigit(calc.charAt(k))) || ((calc.charAt(k))) == '.'); k++)
                {
                    if (calc.charAt(k) == '.')
                    {
                        Board.setText("Error");
                    }
                }
            }
        }
        if (numParenthesis != 0)
        {
            Board.setText("Error");
        }
        else
        {
            Answer = Calculate2(calc);
            Board.setText(Answer);
        }
    }

       
    private String CalculatorImp(String oper1, String oper2, char Oper)
    {
        Double op1, op2;
        double ops1, ops2;
        double ans = 0;
        String result;
       
        op1 = new Double (oper1);
        op2 = new Double (oper2);
        ops1 = op1.doubleValue();
        ops2 = op2.doubleValue();
       
        if (Oper == '+')
        {
            ans = ops1 + ops2;
        }
        if (Oper == '-')
        {
            ans = ops1 - ops2;
        }
        if (Oper == '*')
        {
            ans = ops1 * ops2 ;
        }
        if (Oper == '/')
        {
            ans = ops1/ops2;
        }
       
        result = Double.toString(ans);
        return result;
    }

       
   
    private String Calculate2(String process)
    {
        String answer = process;
        String op1 = "";
        String op2 = "";
        char userinput;
        int index = 0;
        int indexL = 0;
        int indexR = 0;
        int numInput = answer.length();
        int numPar = 0;
        int matchPar = 0;
        int indexOp1 = 0;
        int indexOp2 = 0;
       
        if (answer  != "Error")
        {
            for (index = 0; index < numInput; index++)
            {
                userinput = answer.charAt(index);
               
                if (userinput  == '(')
                {
                    if (matchPar == 0)
                    {
                        indexOp1 = index;
                    }
                    matchPar++;
                    numPar++;
                }
                if (userinput == ')')
                {
                    matchPar--;
                    if (matchPar ==0)
                    {
                        indexOp2 = index;
                    }
                }
            }
            if (indexOp1 + 1 == indexOp2)
            {
                Board.setText("Error");
            }
            if (answer == "Error"  && numPar > 0)
            {
                if (indexOp1 == 0)
                {
                    if (indexOp2 == (numInput - 1))
                    {
                        if (indexOp1 != indexOp2)
                        {
                            answer = Calculate2(answer.substring(indexOp1 + 1, indexOp2));
                        }
                    }
                }
                else if (indexOp1 == 0 && indexOp2 > 0)
                {
                    if ((Character.isDigit(answer.charAt(indexOp2 + 1))))
                    {
                        Board.setText("Error");
                    }
                    else
                    {
                        answer = Calculate2(answer.substring(indexOp1 + 1, indexOp2)) + answer.substring(indexOp2 + 1);
                        numPar--;
                        while (numPar != 0)
                        {
                            answer = Calculate2(answer);
                            numPar--;
                        }
                    }
                }
                else if ((indexOp1 > 0) && (indexOp2 > 0) && (indexOp2 != numInput - 1))
                {
                    if (((Character.isDigit(answer.charAt(indexOp2 + 1 ))) ||  (Character.isDigit(answer.charAt(indexOp1 - 1 ))) ))
                    {
                        Board.setText("Error");
                    }
                    else
                    {
                        answer = answer.substring(0, indexOp1) + Calculate2(answer.substring(indexOp1 + 1, indexOp2)) + answer.substring(indexOp2 + 1);
                        numPar--;
                        while (numPar != 0)
                        {
                            answer = Calculate2(answer);
                            numPar--;
                        }
                    }
                }
                else if (indexOp2 == numInput - 1 && indexOp1 > 0)
                {
                    if (((Character.isDigit(answer.charAt(indexOp1 - 1)))))
                    {
                        Board.setText("Error");
                    }
                    else
                    {
                        answer = answer.substring(0, indexOp1) + Calculate2(answer.substring(indexOp1 + 1, indexOp2));
                        numPar--;
                        while (numPar != 0)
                        {
                            answer = Calculate2(answer);
                            numPar--;
                        }
                    }
                }
            }
            if (numPar == 0)
            {
                if (answer != "Error")
                {
                    if (!(Character.isDigit(answer.charAt(0))))
                    {
                        if (answer.charAt(0) != '-')
                        {
                            if (!(Character.isDigit(answer.charAt(answer.length() - 1))))
                            {
                                Board.setText("Error");
                            }
                        }
                    }
                }
            }
           
            for (index = 0; index < answer.length() && (answer == "Error"); index++)
            {
                userinput = answer.charAt(index);
               
                if (userinput == '*' || userinput == '/')
                {
                    if (!(Character.isDigit(answer.charAt(index-1))) || (!(Character.isDigit(answer.charAt(index + 1)))))
                    {
                        if (answer.charAt(index + 1) != '-')
                        {
                            Board.setText("Error");
                        }
                    }
                    if (answer.charAt(index + 1) == '-')
                    {
                        if (!(Character.isDigit(answer.charAt(index + 2))))
                        {
                            Board.setText("Error");
                        }
                    }
                    if (answer == "Error")
                    {
                        indexL = index - 1;
                       
                        if (indexL > 2)
                        {
                            if ((answer.charAt(indexL - 1)) == '-')
                            {
                               
                                    indexL = indexL -2;
                               
                            }
                        }
                            while ((indexL  > 0) && ((Character.isDigit(answer.charAt(indexL - 2)) || ((answer.charAt(indexL - 1)) == '.'))))
                            {
                                indexL--;
                            }
                            if (indexL == 1)
                            {
                                if ((answer.charAt(indexL - 1)) == '-')
                                {
                                    indexL--;
                                }
                            }
                            if (indexL > 2)
                            {
                                if (((answer.charAt(indexL - 1)) == '-') && !(Character.isDigit(answer.charAt(indexL - 2))))
                                    {
                                        indexL--;
                                    }
                            }
                            op2 = answer.substring(index + 1, indexR + 1);
                         
                            }
                        }
                    }
                }
               
               
                for (index = 0; index < answer.length() && (answer != "Error"); index++)
                {
                    if (index == 0)
                    {
                        index = 1;
                    }
                }
                if (index > 0)
                {
                   
                        if (answer.charAt(index + 1) == '-')
                        {
                            index = index + 2;
                        }
                   
                }
               
                userinput = answer.charAt(index);
               
                if ((userinput == '+') || (userinput == '-'))
                {
                    if (!(Character.isDigit(answer.charAt(index - 1))))
                    {
                        Board.setText("Error");
                    }
                    if (!(Character.isDigit(answer.charAt(index + 1))))
                    {
                        Board.setText("Error");
                    }
                    if ((answer.charAt(index+1) == '-') && (!(Character.isDigit(answer.charAt(index+2)))))
                    {
                         Board.setText("Error");
                    }
                    if (answer != "Error")
                    {
                        indexL = 0;
                        op1 = answer.substring(indexL , index);
                        indexR = index + 1;
                        while((indexR < answer.length()-1) && ((Character.isDigit(answer.charAt(indexR + 1))) || ((answer.charAt(indexR + 1)) == '.')))
                        {
                            indexR++;
                            if (indexR < answer.length() - 2)
                            {
                               
                                    if ((answer.charAt(indexR + 1)) == '-')
                                    {
                                        indexR++;
                                    }
                             
                            }
                        }
                        op2 = answer.substring(index + 1, indexR + 1);
                        answer = CalculatorImp(op1, op2, userinput ) + answer.substring(indexR + 1);
                        index = 0;
                    }
                }
           
       
        return answer;
    }

}
Avatar of gatorvip
gatorvip
Flag of United States of America image

you probably want panel1 to have a GridLayout layout among other things
ASKER CERTIFIED SOLUTION
Avatar of ksivananth
ksivananth
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial