How can I validate that only between numbers

1

This is my code. It only works for me once to validate; if I enter a data type String , when I return to enter a string it gives an error.

public class BienvenidaSegundo {

    static String nombre;
    static int caso;

    public static void main(String[] args) {
        boolean repetir = true;
        while (repetir) {
            try {
                caso = Integer.parseInt(JOptionPane.showInputDialog("Ingrese la manera quiere imprimir la bienvenida(1-scanner,2-Panel)"));
                validar();
                repetir = false;
            } catch (HeadlessException | NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "Cadena ingresada, debe ingresar un valor numerico", "Informacion", JOptionPane.INFORMATION_MESSAGE);
                caso = Integer.parseInt(JOptionPane.showInputDialog("Ingrese la manera quiere imprimir la bienvenida(1-scanner,2-Panel)"));

            }

        }
    }

    private static void operacionPanel() {
        nombre = "";
        while (nombre.equals("")) {
            nombre = JOptionPane.showInputDialog("Ingrese Nombre");
            JOptionPane.showMessageDialog(null, "Bienvenido " + nombre, "Informacion", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    private static void operacionScanner() {
        Scanner entrada = new Scanner(System.in);
        String nombre1 = "";
        while (nombre1.equals("")) {
            System.out.println("Ingrese Nombre");
            nombre1 = entrada.nextLine();
        }
        System.out.println("Bienvenido " + nombre1);

    }

    private static void validar() {
        while (caso < 1 || caso > 2) {
            JOptionPane.showMessageDialog(null, "Ingrese un numero valido", "Error", JOptionPane.ERROR_MESSAGE);
            caso = Integer.parseInt(JOptionPane.showInputDialog("Ingrese la manera quiere imprimir la bienvenida(1-scanner,2-Panel)"));
        }
        switch (caso) {
            case 1:
                operacionScanner();
                break;
            case 2:
                operacionPanel();
                break;
        }
    }
}

The error it throws is:

run: Exception in thread "main" java.lang.NumberFormatException: For input string: "h"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at bienvenidasegundo.BienvenidaSegundo.main(BienvenidaSegundo.java:21)
C:\Users\Hitler\AppData\Local\NetBeans\Cache.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 7 seconds)
    
asked by Agustin Ozuna Leguizamón 04.05.2018 в 02:09
source

1 answer

0

In the main of your class you are repeating an unnecessary instruction for your cycle. That second caso is within the catch and, in turn, is not surrounded by any try-catch that catches the exception, so it just fails.

If you comment | delete the last caso , the cycle is responsible for showing the message again asking for the option to process the name.

public static void main(String[] args) {
        boolean repetir = true;
        while (repetir) {
            try {
                caso = Integer.parseInt(JOptionPane.showInputDialog("Ingrese la manera quiere imprimir la bienvenida(1-scanner,2-Panel)"));
                validar();
                repetir = false;
            } catch (HeadlessException | NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "Cadena ingresada, debe ingresar un valor numerico", "Informacion", JOptionPane.INFORMATION_MESSAGE);
//                caso = Integer.parseInt(JOptionPane.showInputDialog("Ingrese la manera quiere imprimir la bienvenida(1-scanner,2-Panel)"));

            }

        }
    }
    
answered by 04.05.2018 / 02:36
source