Fetch value from a non-static method of the class itself, in a jfieldtextFocusLost method

Asked

Viewed 88 times

0

I have a method created by me that makes the return of a Boolean:

  public boolean ValidaNumero() {
    long valor;
    if(NIPCC.isFocusable()){

    if (NIPCC.getText().length() != 0 ) {
        try {
            valor = Long.parseLong(NIPCC.getText());
            return true;
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(null, "Este campo tem de conter 9 dígitos", "Informação", JOptionPane.INFORMATION_MESSAGE);
            NIPCC.grabFocus();
            return false;

        }
    }
    if(NIPCC.getText().length() != 9){
         JOptionPane.showMessageDialog(null, "Este campo tem de conter 9 dígitos", "Informação", JOptionPane.INFORMATION_MESSAGE);
    }
    return false;
    }
    return true;
}

Now in the default java method I put to validate the number:

private void NIPCCFocusLost(java.awt.event.FocusEvent evt) {                                
    // TODO add your handling code here:


    ValidaNumero();

    if (!NIPCC.getText().substring(0, 1).equals("1") && !NIPCC.getText().substring(0, 1).equals("2")
            && !NIPCC.getText().substring(0, 1).equals("3") && !NIPCC.getText().substring(0, 1).equals("5")
            && !NIPCC.getText().substring(0, 1).equals("6") && !NIPCC.getText().substring(0, 1).equals("8")
            && !NIPCC.getText().substring(0, 1).equals("9")) {
        JOptionPane.showMessageDialog(null, "NIPC inválido!");
    }

The point is that I just want the program to continue if the return of the Validanumero() method is true. I tried to use 'classname.Validationnumber()' to try to know what value was returned, but I am not able to do it. I was thinking something like:

do{ ValidaNumero();while(return false);

There’s a way to do it?

I’m trying to solve another problem which is this::

The idea is to know if what was written in a jFieldText is not characters and if it has exactly 9 digits, but I have a problem:

public void ValidaNumero() {
        long valor;

        if (NIPCC.getText().length() != 9) {
            for (char letra : NIPCC.getText().toCharArray()) {
                if (letra < '0' || letra > '9') {


                }

            }
            NIPCC.requestFocus();
            JOptionPane.showMessageDialog(null, "Este campo tem de conter 9 dígitos", "Informação", JOptionPane.INFORMATION_MESSAGE);
             }
        else{
            System.out.println("Contém 9 dígitos");
        }
    }

When I enter less than two digits or characters, this code is rotated twice and the error message appears twice, you can explain to me why ?

  • from what I understand do{ ValidaNumero();while(return false); executes the method ValidanUmero() only once, is this really the way? If it is, why the cycle (loop)?

  • Because it only runs once, and that’s what I don’t want. I want loop until I get true answer from the Validanumero method().

  • Well I didn’t quite get the idea of the method, but if it every time it runs asks for a value from the user or something like that, then put true in cycle condition. d

  • The Validanumero() method does not ask the user for any value, it simply returns true or false according to some conditions. and I want something that will fetch me the value returned from the method, to say whether it continues the program or not. This does not work but was something like: if(Validanumero() == true) continues the program, Else{Validanumero). I’m not getting the return of the Validation method().

  • But what I don’t understand is this. Imagine the method comes back false and does not start the program, which eventuality could make you change the result?

1 answer

1

From what I saw in the comments, you want something to loop while the Validanumero method is true, correct?

If this is the case, just put the code as follows:

while( ValidaNumero() ) {
   // codigo que você quer que seja executado enquanto validaNumero() for verdadeiro (true)
}

Note that this code only works with its first version of the function Validanumero (the one that returns a Boolean).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.