# If Then Statement Question!

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• October 9th, 2013, 08:53 AM
MLIAKIRA
If Then Statement Question!
Okay so here is my code. For my project we have to calculate the monthly payment for a mortgage. My professor wants us to set it up so when someone enters a negative number for any of the inputs it still lets you enter the rest of them but at the end, the program outputs a message that says "ALL NUMERICAL NUMBERS MUST BE POSITIVE!". I understand the if-then-else statements but I just don't know how to make the program JUST send out the message and not do the calculations if the number is negative. Someone please help! (Also, I know the if statement I have is incorrect, I'm just trying to use an example of what I was saying previously and that the prgram outputs a message but still doees the calculations.)
Code :

```package mortgagecalculation2a;   import java.text.NumberFormat; import java.util.Scanner;   /** * * @author Akira */ public class MortgageCalculation2a {   /** * @param args the command line arguments */ public static void main(String[] args) {   Scanner in = new Scanner(System.in);     double loanAmount; double interestRate; double numberYears; double months; double monthlyPayment;   System.out.print("Enter the loan amount: "); loanAmount = Double.parseDouble(in.nextLine());   if (loanAmount < 0) { System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!"); }   System.out.print("Enter the rate: "); interestRate = Double.parseDouble(in.nextLine());   System.out.print("Enter the number of years: "); numberYears = Double.parseDouble(in.nextLine());   interestRate = interestRate / 100 / 12;   months = numberYears * 12;   monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1);     NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(); System.out.println("The monthly payment is: " +defaultFormat.format(monthlyPayment));   } }```
• October 9th, 2013, 09:10 AM
KevinWorkman
Re: If Then Statement Question!
• October 9th, 2013, 12:38 PM
MLIAKIRA
Re: If Then Statement Question!
I already checked out that website before posting, that is why I posted. I didn't really see the help in it. It doesn't say anywhere how to do the question I asked.

--- Update ---

How do I make the output say the message without it doing the calculations too. All I want is it to recognize that a input is negative and have the output give the message.
• October 9th, 2013, 12:40 PM
KevinWorkman
Re: If Then Statement Question!
Yes, it does.

Did you check out the keyword I told you about? What did you try with it?

In fact, you can do it without that keyword, using only the logic you demonstrated in your original post.

Programming is about experimenting. You aren't going to find an answer for your specific assignment. You have to think about the concepts and reassemble them to solve the problem at hand.
• October 9th, 2013, 12:56 PM
MLIAKIRA
Re: If Then Statement Question!
It basically told me what I already know....
It does not for a fact tell me how I can make it so the message only shows up once as the output and doesn't still do the calculation. I know it's all about experiments but when you've been experimenting for about 2 hours you tend to get a little frustrated..
• October 9th, 2013, 12:57 PM
aussiemcgr
Re: If Then Statement Question!
Given your current design, I believe KevinWorkman was attempting to hint you towards the else statement.
You have an if block for: "if loanAmount is less than 0". In this block, you have the print statement.
You then need to have an else block, for what should happen if the loanAmount is not less than 0. You don't another if statement, since the else block assumes the opposite of the if statement.
**Remember: whatever you put in the else block will NOT execute if the if statement is evaluated to true** (big hint right there)

BTW, by block I am referring to the lines of code between a set of open and closed curly brackets. So the if block is the code between the open and closed curly brackets immediately after the if statement.
• October 9th, 2013, 01:06 PM
MLIAKIRA
Re: If Then Statement Question!
So I what would I put in the else block? Should I put the monthlyPayment equation so that the calculation will not execute then just do a system.out.println of the message? Also, would I have to repeat this process for interestRate and numberYears?
• October 9th, 2013, 01:07 PM
KevinWorkman
Re: If Then Statement Question!
Quote:

Originally Posted by MLIAKIRA
So I what would I put in the else block? Should I put the monthlyPayment equation so that the calculation will not execute then just do a system.out.println of the message? Also, would I have to repeat this process for interestRate and numberYears?

What have you tried?
• October 9th, 2013, 01:10 PM
MLIAKIRA
Re: If Then Statement Question!
Well I tried putting the equation in there...all i got was a red bar so that didnt work and I dont know what else to put so I asked, sorry.
• October 9th, 2013, 01:14 PM
aussiemcgr
Re: If Then Statement Question!
Quote:

Originally Posted by MLIAKIRA
So I what would I put in the else block? Should I put the monthlyPayment equation so that the calculation will not execute then just do a system.out.println of the message? Also, would I have to repeat this process for interestRate and numberYears?

You would put EVERYTHING that you don't want to execute if loanAmount was less than 0. As for the other variables, keep in mind that you can put multiple conditions in an if statement with the use of && (means "and"). So, see if you can rearrange some of your code so that you can check the values of loanAmount, interestRate, and numberYears all in the same if statement.
• October 9th, 2013, 01:14 PM
KevinWorkman
Re: If Then Statement Question!
What red bar?
• October 9th, 2013, 01:23 PM
MLIAKIRA
Re: If Then Statement Question!
Code :

```if (loanAmount < 0){ } else if (monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1)){   System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!");   }```

This is the first thing I tried and there is a red line under the equation and when I hover over it says "incompatible types" and "the assigned value is never used"
• October 9th, 2013, 01:36 PM
aussiemcgr
Re: If Then Statement Question!
Ok, that is incorrect.
You have System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!"); in your else block, not your if block.
"if (monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1))"
Is an if statement, not an if block.

Consider the following code:
Code java:

```int someCondition = 0;   System.out.println("Start of program"); // This will always execute if(someCondition==0) // if statement for: someCondition equals 0 { // start of if block System.out.println("Condition is 0"); } // end of if block else if(someCondition==1) // else if statement for: somecondition equals 1 { // start of if else block System.out.println("Condition is 1"); } // end of if else block else // else statement. The else block is only entered if all previous if and if else statements are evaluated to false { // start of else block System.out.println("Condition is not 0 or 1"); } // end of else block   System.out.println("End of program"); // This will always execute```
If we set someCondition to 0, we get the following output:
Quote:

Start of program
Condition is 0
End of program
If we set someCondition to 1, we get this output:
Quote:

Start of program
Condition is 1
End of program
And if we set someCondition to anything other than 0 and 1, for example: 2, we get this output:
Quote:

Start of program
Condition is not 0 or 1
End of program
• October 9th, 2013, 01:57 PM
MLIAKIRA
Re: If Then Statement Question!
Still confused because you said everything you put in the else if block doesn't execute and if loanAmount and the other variables is less than zero I don't want the equation to be executed.
• October 9th, 2013, 02:03 PM
aussiemcgr
Re: If Then Statement Question!
Ok. For your reasons, you don't need an else if statement or block.
You have two conditions:
1. loanAmount is less than 0
2. loanAmount is not less than 0
So, you need an if statement, and then just an else (no if after that else).

Next, consider the difference between an if statement and an if block.
This is an if statement:
Code java:

`if(someCondition==0) // the statement is between the parentheses`
This is an if block:
Code java:

```{ System.out.println("Condition is 0"); // the block is between the curly brackets: { and } }```

In your code, you had the equation in a statement, not a block.

Do you understand that much?
• October 9th, 2013, 02:03 PM
KevinWorkman
Re: If Then Statement Question!
Take a step back. Write a simple test program that lets you play with if/else statements. Feed in hard-coded booleans, like the example aussiemcgr already posted.
• October 9th, 2013, 02:20 PM
MLIAKIRA
Re: If Then Statement Question!
Quote:

**Remember: whatever you put in the else block will NOT execute if the if statement is evaluated to true** (big hint right there)
Quote:

This is an if block:
{
System.out.println("Condition is 0"); // the block is between the curly brackets: { and }
}
Code :

``` if (loanAmount < 0) {   System.out.println(monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1)); } else if (interestRate < 0) { System.out.println(monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1)); } else if (numberYears < 0) { System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!"); } else { System.out.println(monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1)); }```

I tried it for if, else if and else.

Attachment 2368

Did what you said and the equation still calculated
• October 9th, 2013, 02:32 PM
aussiemcgr
Re: If Then Statement Question!
Ok, that is a step in the correct direction. However, you want this print statement: System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!"); in all the if blocks for a value being less than 0.
There are two ways to approach this:
1. Repeat the statement in each block
2. Combine the if statements into one: if loanAmount<0 OR if interestRate<0 OR if numberYears<0, then print ALL NUMERICAL VALUES MUST BE POSITIVE

The first approach would look like this:
Code java:

```if (loanAmount < 0) { System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!"); } else if (interestRate < 0) { System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!"); } else if (numberYears < 0) { System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!"); } else { System.out.println(monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1)); }```

The second approach would look like this:
Code java:

```if (loanAmount < 0 || interestRate < 0 || numberYears < 0) { System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!"); } else { System.out.println(monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1)); }```
• October 9th, 2013, 02:49 PM
MLIAKIRA
Re: If Then Statement Question!
Okay so I tried option 1 and 2 and it still does the calculation at the end. Also, how would I make it so "The monthly payment is: " not even show up, this is what I was trying to do in the beginning, and just put out the error message just once at the very end where it show the monthly payment, that's what my professors looking for.
Code :

```double loanAmount; double interestRate; double numberYears; double months; double monthlyPayment;   System.out.print("Enter the loan amount: "); loanAmount = Double.parseDouble(in.nextLine());   System.out.print("Enter the rate: "); interestRate = Double.parseDouble(in.nextLine());   System.out.print("Enter the number of years: "); numberYears = Double.parseDouble(in.nextLine());   months = numberYears * 12;   if (loanAmount < 0) { System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!"); } else if (interestRate < 0) { System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!"); } else if (numberYears < 0) { System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!"); } else { System.out.println(monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1)); }   interestRate = interestRate / 100 / 12;   monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1);     NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(); System.out.println("The monthly payment is: " +defaultFormat.format(monthlyPayment));```
Attachment 2370
Please look at it, I honestly have no clue what else to do (also, you can't see it but there is a yellow line under the equation saying that the assigned value is never used)
• October 9th, 2013, 02:58 PM
aussiemcgr
Re: If Then Statement Question!
It does the calculation because you are doing the calculation outside of the else block.
If you do not want the calculation to occur and you don't want the "The monthly payment is" statement to print, all of this code:
Code java:

```interestRate = interestRate / 100 / 12;   monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1);     NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(); System.out.println("The monthly payment is: " +defaultFormat.format(monthlyPayment));```
Needs to be placed inside of the else block.
For reference, the else block I am referring to is this one:
Code java:

```else { System.out.println(monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1)); }```
• October 9th, 2013, 03:08 PM
MLIAKIRA
Re: If Then Statement Question!
Code :

``` if (loanAmount < 0) { System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!"); } else if (interestRate < 0) { System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!"); } else if (numberYears < 0) { System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!"); } else { interestRate = interestRate / 100 / 12;   monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1);   NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(); System.out.println("The monthly payment is: " +defaultFormat.format(monthlyPayment)); }```

Attachment 2371

-_-
• October 9th, 2013, 03:22 PM
MLIAKIRA
Re: If Then Statement Question!
That didn't work.
• October 9th, 2013, 03:23 PM
aussiemcgr
Re: If Then Statement Question!
Can you post your entire code?
• October 9th, 2013, 03:26 PM
MLIAKIRA
Re: If Then Statement Question!
Code :

```package mortgagecalculation2a;   import java.text.NumberFormat; import java.util.Scanner;   /** * * @author Akira */ public class MortgageCalculation2a {   /** * @param args the command line arguments */ public static void main(String[] args) {     Scanner in = new Scanner(System.in);     double loanAmount; double interestRate; double numberYears; double months; double monthlyPayment;       System.out.print("Enter the loan amount: "); loanAmount = Double.parseDouble(in.nextLine());   System.out.print("Enter the rate: "); interestRate = Double.parseDouble(in.nextLine());   System.out.print("Enter the number of years: "); numberYears = Double.parseDouble(in.nextLine());   months = numberYears * 12;   if (loanAmount < 0 || interestRate < 0 || numberYears < 0) { System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!");   } else { interestRate = interestRate / 100 / 12;   monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1);   NumberFormat defaultFormat = NumberFormat.getCurrencyInstance(); System.out.println("The monthly payment is: " +defaultFormat.format(monthlyPayment)); System.out.println(monthlyPayment = interestRate * loanAmount * (Math.pow(1+interestRate, months))/(Math.pow(1+interestRate, months)-1)); }   }       }```

This is my goal for what it should look like..
Attachment 2372
• October 9th, 2013, 03:28 PM
aussiemcgr
Re: If Then Statement Question!
That code cannot create the output you posted in your picture.
I suggest you make sure your file is saved and run it again.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last