vendredi 17 janvier 2020

Using Scanner for Checking if input field has changed & if condition Problem

I've got a Java Problem, I don't understand. I'm using eclipse with Java 10.

My Problem is, that when I enter the 2 Integers for example 99 and 11 it works. But when I enter 11 and 99 in this order, it doesn't work.

And the Scanner for checking if the input has changed doesn't work

I wrote the following Code.

public void euclid (ActionEvent event) {
String userInput1 = input1.getText();
String userInput2 = input2.getText();

if (userInput1.isEmpty() || userInput2.isEmpty()) {
resultLabel.setText("Please insert two integers");
}
if (userInput1.startsWith(" ") || userInput2.startsWith(" ")) {
resultLabel.setText("Please insert two integers");
}

int n1 = Integer.parseInt(input1.getText());
int n2 = Integer.parseInt(input2.getText());
    if (n1 == 0 || n2 == 0) {
        resultLabel.setText("Please insert two integers bigger than 0!");
       }
                  
Scanner scan1 = new Scanner (userInput1);
Scanner scan2 = new Scanner (userInput2);
    if (scan1.hasNextInt() && scan2.hasNextInt()) {
        int gcd = n1 % n2;
        String str = "" + gcd;
        resultLabel.setText(str);
        } else {
            resultLabel.setText("Please insert integers bigger than 0!");
            }
    }

Hopefully, someone can help me with this problem. :)

Aucun commentaire:

Enregistrer un commentaire