I have a method created by me that returns 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 have to do the 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 point is that I just want the program to proceed if the return of the ValidaNumber () method is true. I've tried using 'nomeclasse.ValidaNumero ()' to try to know what value was returned, but I can not do it. I was thinking of something like:
do{ ValidaNumero();while(return false);
Is there a way to do this?
I'm trying to solve another problem that is as follows:
The idea is to know if what was written in a jFieldText are 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 double-clicked and the error message appears twice, can you explain why?