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

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-23-2013, 04:36 PM
MLIAKIRA
How do I ask the user to do the calculation again?
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) : "
• 10-23-2013, 04:43 PM
Norm
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.
• 10-23-2013, 04:49 PM
MLIAKIRA
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.
• 10-23-2013, 05:14 PM
Norm
Re: How do I ask the user to do the calculation again?
Something like this:
Code:

```do{   get data   do calcs   report results   ask user if wants to repeat   get user's response }while(userSaysToRepeat)```
• 10-23-2013, 06:38 PM
MLIAKIRA
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
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
• 10-23-2013, 07:04 PM
Norm
Re: How do I ask the user to do the calculation again?
• 10-23-2013, 07:44 PM
MLIAKIRA
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:

Attachment 5659

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:

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;         String answer;                 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                 answer = in.nextLine();             answer = answer.toLowerCase();                 if ( answer.equals("y"))                     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.
• 10-23-2013, 08:04 PM
Norm
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:
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.
• 10-23-2013, 08:11 PM
MLIAKIRA
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?
• 10-23-2013, 08:29 PM
Norm
Re: How do I ask the user to do the calculation again?
Quote:

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:
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?
• 10-23-2013, 08:39 PM
MLIAKIRA
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.
• 10-23-2013, 08:50 PM
Norm
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.
• 10-23-2013, 09:01 PM
MLIAKIRA
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..
• 10-23-2013, 09:06 PM
MLIAKIRA
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?
• 10-23-2013, 09:12 PM
Norm
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
• 10-23-2013, 09:17 PM
MLIAKIRA
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
• 10-23-2013, 09:21 PM
Norm
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.
• 10-23-2013, 09:24 PM
MLIAKIRA
Re: How do I ask the user to do the calculation again?
Code:

```  if ( answer.equals("y"))                     userInput = true;                             else{                     if (answer.equals("n"))                         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?
• 10-23-2013, 09:30 PM
Norm
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.
• 10-23-2013, 09:39 PM
MLIAKIRA
Re: How do I ask the user to do the calculation again?
Well, i have class in about 10 minutes thanks for nothing.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last