When using recursion method returns 0

2

I have the following code:

public int lerInt() {
    String entrada = "";
    int saida = 0;
    try {
        entrada = input.readLine();
    } catch (IOException e) {
        System.out.println("Falha critica.");
    }
    try {
        saida = Integer.parseInt(entrada);
    } catch (NumberFormatException e) {
        System.out.println("Você digitou uma entrada invalida.");
        lerInt();
    }
    return saida;
}

Assuming the user type something in letter, it would fall into NumberFormatException and would have to type again. However on the second attempt the method returns 0, regardless of the number it enters.

I needed it to run until the user types something valid, and I do not know why it is returning 0.

    
asked by anonymous 06.12.2014 / 01:16

1 answer

3

There are some problems with your code.

I created the variable input there in the same method, you must have created somewhere else. It may even have some reason for you to have done this, but I think inside the method is the best place to declare this variable. I see no point in creating out of it something that seems to be used only inside it. Use the scope better.

There were unnecessary variables. The code is simpler. The only thing you needed to do was try to return an integer value converted from the characters read on the keyboard.

If this attempt fails it will attempt to execute some catch . Not necessarily these two listed there, it could be some other catch in your application's stack (including the% hidden% that every Java application has to protect your application from any unhandled exception).

Normally you only use a single block of catch in these situations. You put how many try s are needed to give the correct treatment for every mistake you know how to treat.

If you give one of these two problems indicated by your code instead of returning the result, before you complete catch it will divert to one of the blocks that know how to handle the problem. In the case both just show the custom message indicating to the user what has gone wrong.

As this is within a loop the whole process will be retried. At the moment everything worked fine the return is executed to the end without a deviation to a return , then the method is finished with the appropriate result.

This catch is called loop infinity. It will run until it's true and I'm saying it's true always. Even though it is infinite, of course there is a way to escape it, which in this case is while .

I made it a little different:

public int lerInt() {
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
    while (true) {
        try {
            return Integer.parseInt(input.readLine());
        } catch (IOException e) {
            System.out.println("Falha critica.");
        } catch (NumberFormatException e) {
            System.out.println("Você digitou uma entrada invalida.");
        }
    }
}

See running on ideone . And no Coding Ground . Also I placed GitHub for future reference .

Note that your attempt to recursion the return method can cause stack overflow problems in extreme cases of many invalid data. Rarely recursion is the solution .

This question should interest you. It explains better about using lerInt() before a return which is practically the same as a finally .

    
06.12.2014 / 01:50