[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeanne Boyarsky wrote:Akira,
You are correct that the try catch block is the problem. Your code flow is basically:
loop
-- enter data
-- if data good, calculate monthly payment with good data
-- if data malformed, calculate monthly payment with whatever data happens to be in the variables
I think you want to skip the entire contents of the loop if the user enters bad data and have them enter again. Where would the catch go to make this happen. Hint: It's a lot lower than now.
Another alternative is to have more loops and not proceed until EACH value entered is value.
Akira belliveau wrote:I've read around the internet that you want to put all the code that has the possibility to catch under the try, is this not what I do?
Akira belliveau wrote:I'm not sure what you mean by moving the the try down more, that would just put it under the calculations?
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeanne Boyarsky wrote:
Right. Because you don't want to do the calculations if the data entered was bad. So you want to skip that whole section and resume after the catch. (in this case the end of the loop to make it so the next thing to happen is re-prompting for new data.)
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Regards,
Sachin
sachin naikwadi wrote:step 1: initialize all the variables at the start of the loop as mentioned by Stuart
step 2: move calculation logic inside try block as it is the dependent code and should only be executed only if all the inputs are valid.
that's it.
sachin naikwadi wrote:step 1: initialize all the variables at the start of the loop as mentioned by Stuart
step 2: move calculation logic inside try block as it is the dependent code and should only be executed only if all the inputs are valid.
that's it.
Akira belliveau wrote:So I put loanAmount = 0, interestRate= 0 and numberYears = 0 at the beginning of the try is that right?
Akira belliveau wrote:Also, by moving the calculations under the try block do you mean put the whole if=then statement under the try because that doesn't work, it gives me a error?
Akira belliveau wrote:Also, I think another problem is I used the wrong exception, I'm not really sure which one I'm supposed to put and what it means. I'm talking about the " NumberFormatException nfe " because I read somewhere I used the wrong exception
Akira belliveau wrote:there is a line under nfe saying the variable is not used.
Stuart A. Burkett wrote:No. Just the contents of the else block. You can then get rid of the if/else statement.
Akira belliveau wrote:If I get rid of the if-then statement my program won't be able to tell if the user enters a negative though and that's a big part of the program.
Stuart A. Burkett wrote:
In that case move the whole if/else statement into the try block. If you get errors post your new code and the exact error messages.
Akira belliveau wrote:1. there is still a line under nfe saying it's not being used
Akira belliveau wrote:
Everything is how I want it to look like but as you can see, the second input still outputs the message "ALL NUMERICAL DATA MUST BE POSITIVE!" when I clearly did not enter a negative number. Anything with my code that might be doing this?
Stuart A. Burkett wrote:
That's because you're not using it. It's only a warning - if you don't want to use nfe in the way I showed then don't. You can just ignore the warning.
Akira belliveau wrote:I've already tried that and it didn't work. I already posted the exact error message above
Stuart A. Burkett wrote:
Have a look at the code I suggested you try and the actual code you used. They are not the same.
You can print your own message as well as the exception message, but you can't do both in the same statement.
Remember that I only suggested doing this to get rid of the 'nfe is not used' warning. If you are happy with the message that is printed, then just leave it as it is and ignore the warning.
The getMessage and printStacktrace methods of the Exception class are useful to know about though as they can help a lot when debugging programs.
Stuart A. Burkett wrote:See the difference. getMessage does not take a parameter, which is what the error message was telling you.
I would say, neither nor.Akira belliveau wrote: . . . Would I put it before or after the System.out.println("You must enter positive numerical data!"); ? . . .