java – Try – Catch y Variable uninitialized

Question:

I just learned the basic structure of the Try-Catch and made a little code to test it, but it gives me an uninitialized variable error. Can someone instruct me a bit about this? I leave the code:

public static void main(String[] args) {
    //Variables
    String nomApel1, nomApel2;
    int edad1, edad2;
    boolean repeat = true;

    while (repeat) {
        //Pidiendo valores de variables al usuario.
        try {
            nomApel1 = JOptionPane.showInputDialog("Nombre y apellido de persona 1: ");
            edad1 = Integer.parseInt(JOptionPane.showInputDialog("Edad de la persona 1: "));
            nomApel2 = JOptionPane.showInputDialog("Nombre y apellido de persona 2: ");
            edad2 = Integer.parseInt(JOptionPane.showInputDialog("Edad de la persona 2: "));
            repeat = false;
        } catch (NumberFormatException ex) {
            System.out.println("Dato inválido, intentalo de nuevo.\n");
        } finally {
            //En todas las variables aquí me dice error de inicialización.
            if (edad1 > edad2) {
                JOptionPane.showMessageDialog(null, "Datos de la persona mayor: \nNombre completo: " + nomApel1 + ".\nEdad: " + edad1 + ".");
            } else {
                JOptionPane.showMessageDialog(null, "Datos de la persona mayor: \nNombre completo: " + nomApel2 + ".\nEdad: " + edad2 + ".");
            }
        }
    }

}

Answer:

The error that it gives you is because you have to give a value to the variables nameApel1, nameApel2, age1 and age 2.

The explanation is very simple, try catch finally is a structure that is used to control the exceptions that can "jump" you.

In your code, imagine that you give an exception when collecting the value of nameApel1 in this statement:

nomApel1 = JOptionPane.showInputDialog("Nombre y apellido de persona 1: ");

By giving an exception, it will go to the catch area (there you have everything fine), but then it will execute the finally area and that is where it will give you problems since, not being able to collect the age data1 , age2 … you will not be able to compare them or show their value.

To avoid this, as the colleague @ Dev.Joel has told you in his answer, you should give some initial value to both age1 and age2 as well as nameApel1 and nameApel2 which can be like this:

public static void main(String[] args) {
//Variables
String nomApel1 = "";
String nomApel2 = "";
int edad1 = 0;
int edad2 = 0;
boolean repeat = true;

while (repeat) {
    //Pidiendo valores de variables al usuario.
    try {
        nomApel1 = JOptionPane.showInputDialog("Nombre y apellido de persona 1: ");
        edad1 = Integer.parseInt(JOptionPane.showInputDialog("Edad de la persona 1: "));
        nomApel2 = JOptionPane.showInputDialog("Nombre y apellido de persona 2: ");
        edad2 = Integer.parseInt(JOptionPane.showInputDialog("Edad de la persona 2: "));
        repeat = false;
    } catch (NumberFormatException ex) {
        System.out.println("Dato inválido, intentalo de nuevo.\n");
    } finally {
        //En todas las variables aquí me dice error de inicialización.
        if (edad1 > edad2) {
            JOptionPane.showMessageDialog(null, "Datos de la persona mayor: \nNombre completo: " + nomApel1 + ".\nEdad: " + edad1 + ".");
        } else {
            JOptionPane.showMessageDialog(null, "Datos de la persona mayor: \nNombre completo: " + nomApel2 + ".\nEdad: " + edad2 + ".");
        }
    }
}

}

Now the problem is that if you have an exception, it will give you the values ​​0 for the int and "" for the String, but you will be able to run the program without problems.

Scroll to Top