3

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.

Pon
  • 41
  • 1
  • 5
  • Probably you use the wrong key constants. Try to use `VK_SLASH` instead of `VK_DIVIDE` and `VK_MINUS` instead of `VK_SUBTRACT` – Sergiy Medvynskyy Sep 14 '17 at 12:12
  • no luck, didn't worked. – Pon Sep 14 '17 at 12:17
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text field. – Andrew Thompson Sep 15 '17 at 00:34

1 Answers1

1

Try keyReleased() instead of keyPressed():

public void keyReleased(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_ENTER) {
        //Do something
}

Some Listeners in Java are pretty bugged, sometimes it saves yourself a lot of headache if you don´t question the reasoning behind it^^

  • Okay now I´m curious, because that method usually works for me. You added the listener to the frame though didn´t you? – Alexander Heim Sep 14 '17 at 12:31
  • I did added it. – Pon Sep 14 '17 at 12:43
  • I think my problem comes when I try to convert it to double because there's a symbol left before the number (ex. /3 ) so when I convert it into double it causes an exception but then it doesnt happen when its like this(ex. -3) it just adds it – Pon Sep 14 '17 at 12:45
  • So you are able to get into the method? Okay then put some output´s in the if-clauses and you will see if you are actually able to enter one of them. When you are able to that I put the clause under comment where you set false. Might be possible that you always enter that one so you will always get false of course. – Alexander Heim Sep 14 '17 at 12:52
  • hey I added an output on all of the if-clauses, they all do their work but still all it does is add. – Pon Sep 14 '17 at 13:08