# Thread: How do I ask the user to do the calculation again?

1. Member Join Date
Oct 2013
Posts
14
Rep Power
0

## How do I ask the user to do the calculation again?

Java Code:
```package MortgageCalculation2b;

import java.text.NumberFormat;
import java.util.Scanner;
/**
*
* @author Akira
*/
public class MortgageCalculation2b {

/**
* @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;

//prompt user for the loan amount
System.out.print("Enter the loan amount: ");
loanAmount = Double.parseDouble(in.nextLine());

//prompt the user for the interest rate
System.out.print("Enter the rate: ");
interestRate = Double.parseDouble(in.nextLine());

//prompt the user for  thenumber of years
System.out.print("Enter the number of years: ");
numberYears = Double.parseDouble(in.nextLine());

//the number of years must be converted to months
months = numberYears * 12;

//if the user enters a negative number print out a error message, if not, continue calculations
if ((loanAmount < 0) || (interestRate < 0) || (numberYears < 0)) {
System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!");

} else {
//convert the interest rate
interestRate = interestRate / 100 / 12;

//monthly payment calculation
monthlyPayment = (interestRate * loanAmount * (Math.pow(1 + interestRate, months))) / (Math.pow(1 + interestRate, months) - 1);

//sytem output
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
System.out.println("The monthly payment is: " + defaultFormat.format(monthlyPayment));

}

}
}```
Basically, this is a mortgage calculation program and I need to add the logic to prompt the user if they wish to enter the data again, then loop if they enter a "y" value; otherwise perform the calcualtions and the error checking. I have no clue how to do this or what to look up, someone help?

I want to ask them "Would you like to calculate again (y/n) : "
Last edited by MLIAKIRA; 10-23-2013 at 05:29 PM.  Reply With Quote

2. ## Re: How do I ask the user to do the calculation again?

Use a do{} while() loop. Ask the user the question at the end of the loop and if the response is not to loop again, exit the loop. If the user wants another go, loop.  Reply With Quote

3. Member Join Date
Oct 2013
Posts
14
Rep Power
0

## Re: How do I ask the user to do the calculation again?

I'm having trouble trying to think of how I would write this. So do { calculations and output } and then while ( System.out.print("Would you like to calculate again (y/n) : ") then would i do a if then inside and do i assign y and n as strings and make them equal to something? i'm confused, sorry.  Reply With Quote

4. ## Re: How do I ask the user to do the calculation again?

Something like this:
Java Code:
```do{
get data
do calcs
report results
ask user if wants to repeat
get user's response
}while(userSaysToRepeat)```  Reply With Quote

5. Member Join Date
Oct 2013
Posts
14
Rep Power
0

## Re: How do I ask the user to do the calculation again?

I'm still no sure what you mean I'm sorry. All I got is
Java Code:
```string userInput = "y' OR "n"
do{my whole code
if (userInput = "y")
my whole code
else
end

while("Would you like to calculate again (y/n) : ")```
pretty much completely confused  Reply With Quote

6. ## Re: How do I ask the user to do the calculation again?  Reply With Quote

7. Member Join Date
Oct 2013
Posts
14
Rep Power
0

## Re: How do I ask the user to do the calculation again?

Okay so i understand it and i got it to work BUT there's a problem after I enter the negative input. Example below: As you can see, after I enter the data the second time, which is the negative number, it doesn't prompt the user if they would like to calculate again and I can't seem to find out why. It always prompts the user if I enter a positive number every time but not when I input a negative. Would you or anyone else know the answer to this? I'm thinking it's something with two if-then statements in the same loop but I'm not sure. Here is my code:

Java Code:
```package MortgageCalculation2b;

import java.text.NumberFormat;
import java.util.Scanner;
/**
*
* @author Akira
*/
public class MortgageCalculation2b {

/**
* @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;
double numerator;
double denominator;
double formula;
boolean userInput;
boolean y;

while (userInput = true) {

//prompt user for the loan amount
System.out.print("Enter the loan amount: ");
loanAmount = Double.parseDouble(in.nextLine());

//prompt the user for the interest rate
System.out.print("Enter the rate: ");
interestRate = Double.parseDouble(in.nextLine());

//prompt the user for  thenumber of years
System.out.print("Enter the number of years: ");
numberYears = Double.parseDouble(in.nextLine());

//the number of years must be converted to months
months = numberYears * 12;

//if the user enters a negative number print out a error message, if not, continue calculations
if ((loanAmount < 0) || (interestRate < 0) || (numberYears < 0)) {
System.out.println("ALL NUMERICAL VALUES MUST BE POSITIVE!");

} else {
//convert the interest rate
interestRate = interestRate / 100 / 12;

//numerator of the monthly payment formula
numerator = Math.pow(1 + interestRate, months);

//denominator of the monthly payment formula
denominator = ((Math.pow(1 + interestRate, months))-1);

//the formula equals the numerator divided by the denominator
formula = ( numerator / denominator );

//monthly payment calculation
monthlyPayment = (interestRate * loanAmount * formula);

//sytem output
NumberFormat defaultFormat = NumberFormat.getCurrencyInstance();
System.out.println("The monthly payment is: " + defaultFormat.format(monthlyPayment));

//prompt the user if they would like to calculate the program again.
System.out.println("Would you like to calculate again? (y/n) : ");
//if the user enters "y" the program will run again and if the user enters anything else it ends
userInput = true;
else{
break;
}

}

}
}}```
Do you see what is causing this problem? Also, could you check if i assigned "userInput" and "y" correctly, I'm not sure if I did? There is a line under them saying the variable is not being used when I hover over it.  Reply With Quote

8. ## Re: How do I ask the user to do the calculation again?

Have you changed what problem you are working on? I thought you were working on allowing the user to repeat the program or exit. See post# for the discussion about how to code a do{}while() loop.

It's better to work on the problems one at a time: design the code, compile it, fix the compiler errors, test it. If it doesn't work start at the beginning and do it again until the test works.

One problem I see here:
Java Code:
` while (userInput = true) {`
is the wrong operator is used. The = is for assigning a value to a variable. The == operator is for comparing the values of two operands.  Reply With Quote

9. Member Join Date
Oct 2013
Posts
14
Rep Power
0

## Re: How do I ask the user to do the calculation again?

Nope, this is the same exact problem but I just got some help from a class mate and this works every time except when i enter the negative number, as I pointed out in that picture, it does not prompt the user if they would want to calculate again. Also, what you said about changing the while to userInput == true, there is a red line and it says that "variable userInput might not have been initialized". I'm not exactly sure why it's saying this but it doesn't work. Also, what I meant about the bottom question is that I was wondering if i declared "userInput" and "y' correctly. Is it supposed to be a boolean or something else?  Reply With Quote

10. ## Re: How do I ask the user to do the calculation again?

variable userInput might not have been initialized"
You have defined a number of variables on lines 19 trhough 29. NONE of them have been given initial values. An example:
Java Code:
`int x = 1;  // define x and give it an initial value`
The compiler REQUIRES variables define in a method be given initial values before their contents are referenced.

What is the purpose of the boolean variable: y?
Last edited by Norm; 10-23-2013 at 07:33 PM.  Reply With Quote

11. Member Join Date
Oct 2013
Posts
14
Rep Power
0

## Re: How do I ask the user to do the calculation again?

The variables "y" and "userInput" are the only variables with this problem and what should I do with the userInput then? before the whiile do I make it userInput = true or something? Also, I user "y" as a boolean because i thought that after I say if answer.equals "y" i was going to do y = TextIO.getBoolean because I read that you should do that when you're doing true and false statements but i guess it's unneeded. Should I just make the y into a string then? Please help I only have an hour until this project is due.
Last edited by MLIAKIRA; 10-23-2013 at 07:45 PM.  Reply With Quote

12. ## Re: How do I ask the user to do the calculation again?

I don't see what need there is for a boolean named: y
The while() loop is controlled by the content of the userInput variable. If its true the code will loop. If it is false the code will exit the while loop. Use the String the user enters as response to the "Would you like to calculate again?" question to set the value of the userInput variable.  Reply With Quote

13. Member Join Date
Oct 2013
Posts
14
Rep Power
0

## Re: How do I ask the user to do the calculation again?

I thought I did that when i said if (answer.equals("y")) then userInput = true? Is that not what I do? What would I do instead? Also, I did String y; and boolean userInput; is that right? Also, none of that answers the part of the code not asking the user if they want to do another calculation after i enter a negative number..  Reply With Quote

14. Member Join Date
Oct 2013
Posts
14
Rep Power
0

## Re: How do I ask the user to do the calculation again?

Basically, what do I type so the problem for y and userInput is gone and how would I fix that problem with te program not asking the user if they would want to do the calculations again?  Reply With Quote

15. ## Re: How do I ask the user to do the calculation again?

Post the code you have for where you read the user's response into a String
and use it to set the value of userInput so that the loop will either exit or continue.
If true continue
if false exit loop  Reply With Quote

16. Member Join Date
Oct 2013
Posts
14
Rep Power
0

## Re: How do I ask the user to do the calculation again?

What? I already posted my code above? Please, I'm begging you, my project is due in like 40 min and I honestly didn't get anything from your past two responses  Reply With Quote

17. ## Re: How do I ask the user to do the calculation again?

Where does the code set the value of userInput so the loop will exit?

The code posted earlier was wrong and I assume that you have changed it as was discussed so far. You need to post the current code so we can see what it is doing.  Reply With Quote

18. Member Join Date
Oct 2013
Posts
14
Rep Power
0

## Re: How do I ask the user to do the calculation again?

Java Code:
```  if ( answer.equals("y"))
userInput = true;
else{
userInput = false;
break;
}```
This? I don't know where to put it! It still shows the line.. Are you not telling me this to mess with me or something?
Last edited by MLIAKIRA; 10-23-2013 at 08:27 PM.  Reply With Quote

19. ## Re: How do I ask the user to do the calculation again?

That looks like the value of userInput would be true with a "y" and false with "n". What should the code do if the answer is neither?

What happens when that code is executed?

The inconsistent use of {}s makes the code dangerous to work with and hard to read and understand.
ALL statements in if and else blocks should ALWAYS be enclosed in {}s.
Last edited by Norm; 10-23-2013 at 08:33 PM.  Reply With Quote

20. Member Join Date
Oct 2013
Posts
14
Rep Power
0

## Re: How do I ask the user to do the calculation again?

Well, i have class in about 10 minutes thanks for nothing.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•