Problem with NumberFormatException and showInputDialog

2

My problem is that I have a window JOptionPane.showInputDialog that receives a data of type int . I wanted to do a try-catch to handle the exception NumberFormatException if they put a letter instead of a number. The problem I have is that if you close or select cancel in showInputDialog , it also treats it as a NumberFormatException since a int can not be null. How can I treat these exceptions separately?

Here is the code snippet:

do{
    try{
        calcular.pedir_precio_materiales(
            Integer.parseInt(
                JOptionPane.showInputDialog("¿Cual es el coste total de los materiales?")
            )
        );
    }catch(NumberFormatException exc){
        excepción_de_formato=true;
        System.out.println("Valor incorrecto, intente de nuevo");
    }
}while(excepción_de_formato);

PD: calcular.pedir_precio_materiales() is a method of another class and excepción_de_formato is a boolean that I stated previously.

    
asked by Alejandro Arevalo 19.03.2017 в 00:00
source

4 answers

0

Try this:

do {
    String stringInput = JOptionPane.showInputDialog("Entre un número");
    try {
        int numero = Integer.parseInt(stringInput);
        calcular.pedir_precio_materiales(numero);

    } catch (NumberFormatException e) {
        System.out.println ("El valor introducido no es un número.");
    }
} while (excepcion_de_formato);
    
answered by 19.03.2017 / 00:52
source
0

There is no separate exception, since trying to convert something null to a int is also NumberFormatException , however you can do it next to try different null .

String a;
            do{
        try{
            a=JOptionPane.showInputDialog("Numero :");
            if(a==null)
           JOptionPane.showMessageDialog(null,"El valor es Nulo");
           else
            calcular.pedir_precio_materiales(Integer.parseInt(a));
        }
        catch(NumberFormatException e){
        JOptionPane.showMessageDialog(null,"Caracteres Invalidos");
        excepcion_de_formato=false;
        }
    }while(excepcion_de_formato);
    
answered by 19.03.2017 в 00:34
0

What happens is that if you click if or if you need a value your try that's why you solve it in catch. Initialize your int before. for example:

try{
 int a=Integer.parseInt(JOptionPane.showInput.....);
            calcular.pedir_precio_materiales(a);
            }catch(NumberFormatException exc){
                excepción_de_formato=true;
                System.out.println("Valor incorrecto, intente de nuevo");
            }
    
answered by 19.03.2017 в 00:19
0

That's what I would do:

boolean sigue = true;
while (sigue){
    String str = JOptionPane.showInputDialog("Introduce número: ");
    sigue = str != null;
    if (sigue) {
        try {
            int num = Integer.parseInt(str);
            calcular.pedir_precio_materiales(num);
        } catch (NumberFormatException e) {
            System.out.println ("El valor introducido no es un número.");
        }
    }
}
    
answered by 19.03.2017 в 06:51