• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Try-Catch block issues

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So we have to make a mortgage calculation project where we have to ask the user to calculate again and now we have to make it so it prints out a error message every time the user enters a string value for any of the inputs. I thought I did it right but something strange happens every time I run it and I can't figure out why and I know it's something wrong with the Try-Catch blocks.

Here are my outputs:


As you can see the third time i run the program I enter a "two" as the second input and it still did the calculations. Then, the third time I tried it, I entered a negative number then a "two" and everything worked the way I wanted it to. Then, the last time I ran it I put a positive number for the first input and it still did the calculations, anything you guys see that might be doing this?

Here's my code:

Is there anything that you guys can see that might be the problem?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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? I'm not sure what you mean by moving the the try down more, that would just put it under the calculations?
 
Akira belliveau
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



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? I'm not sure what you mean by moving the the try down more, that would just put it under the calculations?
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?


Kind of. The minimum amount of code in a try is the code that has the possibility to catch. You sometimes need more though.

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?


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.)
 
Akira belliveau
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.)



Would I put finally?
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. You only need finally if you need to clean up resources or do something regardless of whether an exception is thrown.
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first time through the loop you assign values to loanAmount, interestRate and numberYears variables.
Each subsequent time through the loop, these variables will be given new values unless an exception is thrown. If an exception is thrown, the variables will retain their previous values (they will not be set to 0) so your test on line 51 fails.
You need to reset all the variables to 0. Either at the start of the loop or in the catch block.
 
Greenhorn
Posts: 7
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



So I put loanAmount = 0, interestRate= 0 and numberYears = 0 at the beginning of the try is that right? 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? Would I just copy and paste the calculations under the try and leave the calculations under the if-then statement to? 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 ".
 
Akira belliveau
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



So I put loanAmount = 0, interestRate= 0 and numberYears = 0 at the beginning of the try is that right? 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? Would I just copy and paste the calculations under the try and leave the calculations under the if-then statement to? 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 and there is a line under nfe saying the variable is not used.
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akira belliveau wrote:So I put loanAmount = 0, interestRate= 0 and numberYears = 0 at the beginning of the try is that right?


Yes

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?


No. Just the contents of the else block. You can then get rid of the if/else statement.

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


It's the right exception.

Akira belliveau wrote:there is a line under nfe saying the variable is not used.


That's because you don't use it in the exception block. Rather than printing your own message try using

or even just
 
Akira belliveau
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:No. Just the contents of the else block. You can then get rid of the if/else statement.



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. Also, is my exception correct and if not what would I use?
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.


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.

I edited my previous post with a response to your question about the exception.
 
Akira belliveau
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.







Also, there is a red line under the nfe.getmessage and it says "method getMessage in class Throwable cannot be applied to given types;
requirement: no arguments
found: string
reason: actual and formal argument lists differ in length "
 
Akira belliveau
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I changed my code but there is still a couple of problems.
1. there is still a line under nfe saying it's not being used
2. something with the outputs
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
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akira belliveau wrote:1. there is still a line under nfe saying it's not being used


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:
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?


You are checking if the variables are less than or equal to zero. If an exception was thrown then at least one of your variables will be 0 so it will print the message.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, that behaviour is correct. 0 is not a valid amount for a mortgage or a repayment.

You do realise that entering -123.45 a a double will not cause an NFE to be thrown. If you receive non‑positive amounts, you should consider an IllegalArgumentException.
 
Akira belliveau
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



I've already tried that and it didn't work. I already posted the exact error message above
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akira belliveau wrote:I've already tried that and it didn't work. I already posted the exact error message above


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.
 
Akira belliveau
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.



I did the getMessage but I didn't put it in the code i posted. When I put it there there was a red line under the nfe.getmessage and it says "method getMessage in class Throwable cannot be applied to given types;
requirement: no arguments
found: string
reason: actual and formal argument lists differ in length "
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I suggested you try was

What you put in your code was

See the difference. getMessage does not take a parameter, which is what the error message was telling you.
 
Akira belliveau
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not really sure what you mean. Do you want me to put the first or second thing you recommended me? I put both and it still give me a error
 
Akira belliveau
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stuart A. Burkett wrote:See the difference. getMessage does not take a parameter, which is what the error message was telling you.



Would I put it before or after the System.out.println("You must enter positive numerical data!"); ?

Either way I put the statement it outputs " For input string: "two" " and I don't want that. It did in fact fix the line that said the variable is not being used.

EDIT: I tried the example my teacher used which was " System.out.println("You must enter positive numerical data!" + e.getLocalizedMessage()); " and I still get the same thing. It outputs " For string : whatever the user inputs "
 
Campbell Ritchie
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akira belliveau wrote: . . . Would I put it before or after the System.out.println("You must enter positive numerical data!"); ? . . .

I would say, neither nor.

There is a far better way to get keyboard input, viz. a utility class which does the input reading for you. You can call it like this:-If you use a Scanner instance and its hasNextInt() method, you can write loops which discard any non‑int inputs, and you can use those numbers to ensure the input is in that particular range. I have posted that sort of thing several times, and if you search my posts for utility class or Scanner hasNextInt, …






…you will find this post

You will also find incomplete examples of such utility classes, but you can probably assemble a complete class if you find enough pieces. Then you can write your own methods. Beware of using nextLine after nextAnythingElse, however. There is a pitfall there.
 
reply
    Bookmark Topic Watch Topic
  • New Topic