There are some problems in your code.
I created the variable input
There in the same method, you must have created elsewhere. There may even be some reason you did this, but I think within the method is the best place to declare this variable. I see no point in creating something outside of it that seems to be used only within it. Better use the scope.
There were unnecessary variables. The code is simpler. The only thing you needed to do was try to return a converted integer value of the characters read on the keyboard.
If this attempt fails she will try to perform some catch
. Not necessarily these two listed there, could be some other catch
in the call stack its implementation (including the catch
hidden that every Java application has to protect your application from any untreated exceptions).
Normally you only use a single block of try
in these situations. You put how many catch
es are necessary 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 completing the return
He will divert to one of the blocks that know how to treat the problem. In case both just show the custom message indicating to the user what went wrong.
How this is inside a loop the whole process will be tried again. By the time everything went right the return
is executed to the end without a diversion to a catch
, then the method is finished with the appropriate result.
This while
is called loop infinite. It will run until it is true and I am saying it is true always. Even though it is infinite, of course there is a way to escape it, which in this case is the return
.
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.");
}
}
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Note that your attempt to recur the method lerInt()
can cause stack overflow problems in extreme cases of many invalid data. Rarely recursion is the solution.
That question should interest you. She explains better about the use of return
before a finally
which is practically the same thing as a catch
.
I created the input in the class, there are other methods that use it. and I can’t create the static method (even wanted) because of UML
– athosbr99
your code worked but so far not understood the
while (true)
. until what is true?– athosbr99
I put
static
just to test. It doesn’t matter if you’re going to use this variable elsewhere, unless you explain why you need it doesn’t make sense to have it shared across the entire class. Awhile
expecting atrue
orfalse
, you don’t need to compare anything to "something" to get these values. And please, never compare "something" totrue
orfalse
, if this "something" is already a boolean type, use this "something" directly.– Maniero
I took a closer look and understood what happened. but although now I realized that my approach was not correct I did not understand why it returned 0, the method should not return what happened in the last recursion?
– athosbr99
No, he should return what he executed on the first run. The last one and all the others are being thrown away by his code. Maybe a
return lerInt();
get what you want but I wouldn’t risk it.– Maniero