J
J
joha07382021-05-28 18:34:12
Java
joha0738, 2021-05-28 18:34:12

Why the program does not want to execute the method called through the keyboard and how to fix it?

There is a method

public void number (String n) {
        if (Result.getText().length() < 14) {
            wer[0] = wer[0] + n;
            Result.setText(wer[0]);
        }
    }

which displays
@FXML
    private Text Result = new Text();
numbers entered by the user.
if this method is accessed by clicking on the buttons in the application, for example
btn1.setOnAction(actionEvent -> {
            number("1");
        });

then everything works as intended, but if you access the same method through the keyboard
public void sd(KeyCode q){
        switch (q){
            case NUMPAD0: number("0"); break;
            case NUMPAD1: number("1"); break;
            case NUMPAD2:  break;
            case NUMPAD3:  break;
            case NUMPAD4:  break;
            case NUMPAD5:  break;
            case NUMPAD6:  break;
            case NUMPAD7:  break;
            case NUMPAD8:  break;
            case NUMPAD9:  break;
            case ADD: plus(); break;
            default: System.out.println("NOT FOUND: "+q);;
        }
    }

then it simply ignores and does nothing and does not display any error if replaced with break; then everything works. and the desired number appears in the terminal. case NUMPAD0: number("0"); break;case NUMPAD0: System.out.println("0"); break;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-05-28
@joha0738

Obviously you have a problem with this method:

public void number (String n) {
        if (Result.getText().length() < 14) {
            wer[0] = wer[0] + n;
            Result.setText(wer[0]);
        }
    }

For example, when a condition is called, the if branch is not executed.
It's also not very clear how you call the method number();if it's non-static. Do you have an instance of a class?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question