Java field with numbers only

Asked

Viewed 458 times

-2

I’m doing a medical record for a hospital system test and I’d like to know how to validate the field CRM. It is a variable double, I want the field of Joptionpane accepted only numbers in CRM, I’ve tried several ways regular Expression etc and nothing worked out..

public static void cadastroMedico(){
    int lista=1;
    do{
        acho=0;
        codigoMedic=Integer.parseInt(JOptionPane.showInputDialog(null, "Digite o código do médico"));
        if(medicos.size()>0)
            for(int i = 0;i<medicos.size();i++){
                if(codigoMedic==medicos.get(i).getIdMedico())
                    acho=1;
            }
            if(acho == 1)
                JOptionPane.showMessageDialog(null, "Médico já cadastrado","Aviso",1);        
    }while(acho==1);

    nomemed = JOptionPane.showInputDialog(null, "Digite o nome do Médico: ");
    while(!matchesOnlyText(nomemed)) {
        JOptionPane.showMessageDialog(null, "Você não pode inserir números no campo nome.");
        nomemed = JOptionPane.showInputDialog(null, "Digite o nome do Médico: ");
    }

    **crm = Double.parseDouble(JOptionPane.showInputDialog(null, "Digite o CRM do Médico: "));**


    setor = JOptionPane.showInputDialog(null, "Digite o setor do Médico: ");
    while(!matchesOnlyText(setor)) {
        JOptionPane.showMessageDialog(null, "Você não pode inserir números no campo setor.");
        setor = JOptionPane.showInputDialog(null, "Digite o setor do Médico:");
    }
}
  • When you say you want to validate CRM you mean format validation or validation of a doctor’s existence with this CRM number?

  • In the case I’m talking about format validation, but the most priority part in the case would be "I want the Joptionpane field to accept only CRM numbers".

  • I am not finding the format of a CRM number, could indicate?

  • An example of CRM : 27134.

  • because it needs to be double?

  • Regex to only be valid digits [0-9]+$

  • 1

    You’re right, I changed the CRM variable to Int, but it still doesn’t work.

Show 2 more comments

1 answer

0


See if this is what you want :

String input;
int crm;
do {
    input = JOptionPane.showInputDialog("Digite o CRM do Médico: ");
    if (input.matches("^[0-9]+$")) {
        crm = Integer.parseInt(input);
        System.out.println("CRM : " + crm);
    } else {
        System.out.println("Por favor insira um CRM válido");
    }
} while (!input.matches("^[0-9]+$"));

Browser other questions tagged

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