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

I have a method created by me that does 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 Java's default method pus to do number validation:

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 issue is that I just want the program to proceed in case the return of the Validanumber() method is true. I tried using ' className.ValidaNumero()' to try to know what value was returned, but I am not able to do. I was thinking something like:

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

There is a way of do?

I'm trying to solve another problem which is the following:

The idea is to know if what was written in a jFieldText are not characters and if it has exactly 9 digits, but I'm having 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, can you explain why ?

Author: HugoMachado, 2014-10-16

1 answers

From what I saw in the comments, do you want something to be looped as long as 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 your first version of the Validanumber function (the one that returns a boolean).

 1
Author: Raphael do Vale, 2014-10-20 23:55:01