Desenvolva um programa que entre com nome e uma matrícula. Seu programa deve validar o nome e a matrícula.

31/05/2019

0

Olá a todos. Criei esse código e consegui executar a primeira tela. Mas preciso VALIDAR o Nome e a Matrícula e dá erro. Desde já agradeço. Segue o código:

package br.com.ead.matricula;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class AlunoTeste2 {

public static void main(String[] args) {

JTextField nome = new JTextField();
JTextField matricula = new JTextField();


Object[] escola = {
"Nome:", nome,
"Matrícula:", matricula,
};

JOptionPane.showConfirmDialog(null, escola,"Validação de Alunos- PROJEÇÃO", JOptionPane.OK_CANCEL_OPTION);

------------------------------------------------------------

A seguir criei esse código para tentar validar os campos mas não consigo "amarrar" os códigos, segue:

if(escola == null) {
JOptionPane.showMessageDialog(null, "Você pressionou CANCEL");
}
if (escola!=null) {
try {
int matricula = Integer.parseInt(escola);

if (matricula>=200010000 && matricula<202000000) {
JOptionPane.showMessageDialog(null, "Matrícula Confere "+"\\n"+matricula);
}
else {
JOptionPane.showMessageDialog(null, "Matrícula errada!");
}
}catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Entrada Errada");
}
}
Michael

Michael

Responder

Post mais votado

01/06/2019

package br.com.ead.matricula;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class AlunoTeste2 {

    public static void main(String[] args) {

        JTextField nome = new JTextField();
        JTextField matricula = new JTextField();

        Object[] escola = {
            "Nome:", nome,
            "Matrícula:", matricula,	
        };	
        
       
        boolean vr = true; //verificador para sair do laço de repetição
        do{    
            JOptionPane.showConfirmDialog(null, escola,"Validação de Alunos- PROJEÇÃO", JOptionPane.OK_CANCEL_OPTION);
            int matriculaNum;
            try {               
                matriculaNum = Integer.parseInt(matricula.getText());
                System.out.println(matriculaNum);

                if (matriculaNum>=200010000 && matriculaNum<202000000) {
                    JOptionPane.showMessageDialog(null, nome.getText() + " Matrícula Confere "+"\\n"+matriculaNum);
                    vr = false;
                }
                else {
                    JOptionPane.showMessageDialog(null, "Matrícula errada!");
                }
            }catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Entrada Errada");
            }
        } while (vr);
        JOptionPane.showMessageDialog(null, "Fim!");
    }
}

Kleber Santos

Kleber Santos
Responder

Mais Posts

01/06/2019

Michael

package br.com.ead.matricula;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class AlunoTeste2 {

    public static void main(String[] args) {

        JTextField nome = new JTextField();
        JTextField matricula = new JTextField();

        Object[] escola = {
            "Nome:", nome,
            "Matrícula:", matricula,	
        };	
        
       
        boolean vr = true; //verificador para sair do laço de repetição
        do{    
            JOptionPane.showConfirmDialog(null, escola,"Validação de Alunos- PROJEÇÃO", JOptionPane.OK_CANCEL_OPTION);
            int matriculaNum;
            try {               
                matriculaNum = Integer.parseInt(matricula.getText());
                System.out.println(matriculaNum);

                if (matriculaNum>=200010000 && matriculaNum<202000000) {
                    JOptionPane.showMessageDialog(null, nome.getText() + " Matrícula Confere "+"\\\\\\\\\\\\\\\\n"+matriculaNum);
                    vr = false;
                }
                else {
                    JOptionPane.showMessageDialog(null, "Matrícula errada!");
                }
            }catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Entrada Errada");
            }
        } while (vr);
        JOptionPane.showMessageDialog(null, "Fim!");
    }
}





Entendi o conceito Kleber. Mas a validação do NOME eu posso colocar dentro desse Try-catch?
Responder

01/06/2019

Kleber Santos

package br.com.ead.matricula;

import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class AlunoTeste2 {

    public static void main(String[] args) {

        JTextField nome = new JTextField();
        JTextField matricula = new JTextField();

        Object[] escola = {
            "Nome:", nome,
            "Matrícula:", matricula,	
        };	
        
       
        boolean vr = true; //verificador para sair do laço de repetição
        do{    
            JOptionPane.showConfirmDialog(null, escola,"Validação de Alunos- PROJEÇÃO", JOptionPane.OK_CANCEL_OPTION);
            int matriculaNum;
            try {               
                matriculaNum = Integer.parseInt(matricula.getText());
                System.out.println(matriculaNum);

                if (matriculaNum>=200010000 && matriculaNum<202000000) {
                    JOptionPane.showMessageDialog(null, nome.getText() + " Matrícula Confere "+"\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n"+matriculaNum);
                    vr = false;
                }
                else {
                    JOptionPane.showMessageDialog(null, "Matrícula errada!");
                }
            }catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(null, "Entrada Errada");
            }
        } while (vr);
        JOptionPane.showMessageDialog(null, "Fim!");
    }
}


Entendi o conceito Kleber. Mas a validação do NOME eu posso colocar dentro desse Try-catch?



import javax.swing.JOptionPane;
import javax.swing.JTextField;
 
public class teste{
 
    public static void main(String[] args) {
 
        JTextField nome = new JTextField();
        JTextField matricula = new JTextField();
 
        Object[] escola = {
            "Nome:", nome,
            "Matrícula:", matricula,    
        };  
         
        
        boolean vr = true; //verificador para sair do laço de repetição
        do{    
            JOptionPane.showConfirmDialog(null, escola,"Validação de Alunos- PROJEÇÃO", JOptionPane.OK_CANCEL_OPTION);
            int matriculaNum;
            try {               
                matriculaNum = Integer.parseInt(matricula.getText());
                System.out.println(matriculaNum);
                System.out.println("nome " + nome.getText());
                if (!"".equals(nome.getText())){ //se o campo nome.getText() for diferente de "" (nada) execute
                    if (matriculaNum>=200010000 && matriculaNum<202000000) {
                        JOptionPane.showMessageDialog(null, nome.getText() + " Matrícula Confere "+"\\n"+matriculaNum);
                        vr = false;
                    }
                    else {
                        JOptionPane.showMessageDialog(null, "Matrícula errada!");
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Coloque Nome");
                }
            }catch (NumberFormatException e) {
                //
                JOptionPane.showMessageDialog(null, "CATCH - digite um numero na matricula");
            }
        } while (vr);
        JOptionPane.showMessageDialog(null, "Fim!");
    }
}

Michael o Try-catch é uma forma de você trabalhar os erros do código.
Nesse código ele vai executar o catch caso seja digitado por exemplo uma letra no campo matricula, assim na linha "matriculaNum = Integer.parseInt(matricula.getText());" ele vai tentar converter essa letra em numero e vai ocasionar um erro que vai ser tratado dentro do catch.
Responder

Que tal ter acesso a um e-book gratuito que vai te ajudar muito nesse momento decisivo?

Ver ebook

Recomendado pra quem ainda não iniciou o estudos.

Eu quero
Ver ebook

Recomendado para quem está passando por dificuldades nessa etapa inicial

Eu quero

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar