Reputation: 41
I am trying to prompt a user for input of Y, y or N, n and using a while loop to ask if they want to rerun a program. I have tried using the code that worked from this question. I changed the String choice = sc.next(); to in.next(); When I run the program everything works fine, but when prompted and I answer y it will only ask if I want to run again without actually running the program (letting user input and outputting answer). The n will work fine and end it. Thank you for your time! This has kept me up for many nights scouring for an answer on here, youtube, and other coding forums.
Edit: I tried moving the loan-related code into the while loop (between while and boolean) , but now it entirely skips "Would you like to calculate again (y/n): " and goes straight into "Enter loan amount: " without asking the
package numformexception;
import java.util.Scanner;
public class NumFormException {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//Declare Variables
double loanNum;
double rateNum;
double trateNum;
double yearNum;
double monthNum;
double equNum;
double outputNum;
//Prompt for input
System.out.print("Enter loan amount: ");
loanNum = Double.parseDouble(in.nextLine());
System.out.print("Enter rate: ");
rateNum = Double.parseDouble(in.nextLine());
System.out.print("Enter number years: ");
yearNum = Double.parseDouble(in.nextLine());
//Postive input data check
if(loanNum <= 0 || rateNum <=0 || yearNum <=0)
{
System.out.println("You must enter positive numeric data! ");
System.exit(0);
}
//Convert years to months
monthNum = yearNum * 12;
//Convert rate to percent and monthly
trateNum= (rateNum / 100) /12;
//Complete numerator
equNum = Math.pow((1+ trateNum), monthNum );
//Plug equNum into formula
outputNum = trateNum * loanNum * (equNum / (equNum - 1));
System.out.printf("The monthly payment is: $ %.2f%n", outputNum);
boolean isContinuing = true;
while( isContinuing) {
boolean inputIsInvalid = true;
while (inputIsInvalid) {
System.out.print("Would you like to calculate again (y/n): ");
String choice = in.next();
if("y".equalsIgnoreCase(choice)) {
inputIsInvalid = false;
}
else if("n".equalsIgnoreCase(choice)){
inputIsInvalid = false;
isContinuing = false;
}
else {
System.err.print("Error: Only valid answers are Y/N. ");
}
}
}
}
}
Upvotes: 1
Views: 770
Reputation: 124704
Your main calculation will not run again, because it's not part of this loop:
while (isContinuing) {
// ...
}
I suggest to move the main calculation logic into a method, for example:
private static void calculateLoan(Scanner in) {
//Declare Variables
double loanNum;
double rateNum;
double trateNum;
double yearNum;
double monthNum;
double equNum;
double outputNum;
//Prompt for input
System.out.print("Enter loan amount: ");
loanNum = Double.parseDouble(in.nextLine());
System.out.print("Enter rate: ");
rateNum = Double.parseDouble(in.nextLine());
System.out.print("Enter number years: ");
yearNum = Double.parseDouble(in.nextLine());
//Postive input data check
if (loanNum <= 0 || rateNum <= 0 || yearNum <= 0) {
System.out.println("You must enter positive numeric data! ");
System.exit(0);
}
//Convert years to months
monthNum = yearNum * 12;
//Convert rate to percent and monthly
trateNum = (rateNum / 100) / 12;
//Complete numerator
equNum = Math.pow((1 + trateNum), monthNum);
//Plug equNum into formula
outputNum = trateNum * loanNum * (equNum / (equNum - 1));
System.out.printf("The monthly payment is: $ %.2f%n", outputNum);
}
And then change the main
method in way that it will call the new calculateLoan
method inside the loop:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean isContinuing = true;
while (isContinuing) {
calculateLoan(in);
boolean inputIsInvalid = true;
while (inputIsInvalid) {
System.out.print("Would you like to calculate again (y/n): ");
String choice = in.next();
if ("y".equalsIgnoreCase(choice)) {
inputIsInvalid = false;
} else if ("n".equalsIgnoreCase(choice)) {
inputIsInvalid = false;
isContinuing = false;
} else {
System.err.print("Error: Only valid answers are Y/N. ");
}
}
}
}
Upvotes: 1
Reputation: 285405
The only code that will loop is that which is within your while loop. So it makes complete sense that none of the loan-related code will loop. If you want it to loop, you must first put it in a loop, likely within the while continuing while loop, one that should enclose almost everything in your main method, everything after the variable declaration.
Upvotes: 0