Calculator using KeyListener JAVA

Viewed 1071

I'm beginner in programming so please bear with me, I'm trying to make a Calculator using a KeyListener and this is what I've got(cutted code)

    public void keyPressed(KeyEvent e){
    int code = e.getKeyCode();
    System.out.println("   Code: " + KeyEvent.getKeyText(code));

    if(KeyEvent.VK_ADD == e.getKeyCode()){
        tmp = Double.parseDouble(txtDisplay.getText());
        txtDisplay.setText(display);
        add = true;

    }
    if(KeyEvent.VK_DIVIDE == e.getKeyCode()){
        tmp = Double.parseDouble(txtDisplay.getText());
        txtDisplay.setText(display);
        div = true;
    }
    if(KeyEvent.VK_SUBTRACT == e.getKeyCode()){
        tmp = Double.parseDouble(txtDisplay.getText());
        txtDisplay.setText(display);
        sub = true;
    }
    if(KeyEvent.VK_MULTIPLY == e.getKeyCode()){
        tmp = Double.parseDouble(txtDisplay.getText());
        txtDisplay.setText(display);
        mul = true;
    }
    if(e.getKeyCode()==KeyEvent.VK_BACK_SPACE){
        txtDisplay.setText("");
        tmp = 0;
        toSolve = 0;
    }
    if(e.getKeyCode() == KeyEvent.VK_ENTER){
        toSolve = Double.parseDouble(txtDisplay.getText());
        if( add == true ){
            toSolve = tmp + toSolve;}

        else if ( sub == true  ){

            toSolve = tmp - toSolve;}

        else if ( mul == true  ){

            toSolve = tmp * toSolve;}

        else if ( div == true  ){

            toSolve = tmp / toSolve;}

    txtDisplay.setText(Double.toString( toSolve ));

    add = false ;
    sub = false ;
    mul = false ;
    div = false ;

    }


}

I want it to perform the operations but when I run it it seems that it's only doing the addition. I think it might be in my programming logic. Also how can I remove the operation symbol in the textfield because the it also convert the symbol so it throws an exception.

1 Answers
Related