If Else loop or do while loop
What I'm trying to do is say the user types in -100000 for a loan amount
or "two" for the rate. I'm trying to give them an error message and put them back into putting in the loan amount or rate.
And if they enter positive information i'd ask them if they would like to calculate again. if they choose Y then they go back into the loop. if they choose n then i break it. I was wondering how do I do this, and would it be a if else loop or a combination of both if do and while loops?
Also I am very new at Java.. It would help if you guys typed out some of the code or typed an example of it so I at least had guidelines to follow.
Any help would be appreciated!
Basically I have this code:
Code Java:
package monthlypayment3a;
import java.util.Scanner;
public class Monthlypayment3a {
private static double monthlypay1;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double months = 1;
int loan;
int numyears;
double rate;
System.out.print("Enter Loan Amount: ");
loan = in.nextInt();
System.out.print("Enter Rate: ");
rate = in.nextDouble();
System.out.print("Enter number of years: ");
numyears = in.nextInt();
double calc1 = loan * rate;
double calc2 = Math.pow(1 + rate, months);
double calc3 = Math.pow(1 + rate, months)-1;
double calc4 = calc1 / calc2;
monthlypay1 = calc4 / calc3;
System.out.print("The Monthly Payment is: $ " + monthlypay1);
}
}
Re: If Else loop or do while loop
Any help?
--- Update ---
Guys please. I really need help on this. What I've tried is this.
Code Java:
int repeat = 0;
int error = 0;
do {
if (error == true) {
System.out.print("You must enter Positive Numerica Data!"); }
else {
System.out.print("Enter Loan Amount: ");
loan = in.nextInt();
}
What I'm trying to do is if the user enters a number less than 0 in the loan amount I will prompt them to enter a positive number.
And put them back at the beginning of the loop.
I get the error boolean and int are incompatible.
So I tried this
Code Java:
int repeat = 0;
int error = 0;
while (error <= 0 ) {
if (error == 0) {
System.out.print("You must enter Positive Numerical Data!"); }
else {
System.out.print("Enter Loan Amount: ");
loan = in.nextInt();
}
When I run it it doesn't do the loop condition. It just prints out you must enter positive numerical data...
Re: If Else loop or do while loop
Ok pseudocode:
Code :
do
get user input
if user input not ok
display error message
while user input not ok
Re: If Else loop or do while loop
I put the statements in the incorrect place and tried this.
Code Java:
if (monthlypay1 >= 0) {
System.out.print("The Monthly Payment is: $ " + monthlypay1);
System.out.print("Would you like to calculate again? (Y/N)"); }
else {
System.out.print("You need to enter Positive Numerical Data!");}
It works. Now can anyone tell me how to loop the user back into the program if they enter a negative number? And also how to prompt the user the error message if they enter a string? like "two"
--- Update ---
Anyfeedback?
Re: If Else loop or do while loop
Quote:
how to loop the user back into the program
Use a while() loop
BTW The code should NOT hide }s at the end of statements. }s should mostly be on a line by themselves in column with the line with the pairing {
Re: If Else loop or do while loop
Umm you could use a while loop inside a while loop, so basically if the user enters the loop it would execute the statements inside and then move onto the inner loop (which would also execute their statements if the condition is true).
Re: If Else loop or do while loop
Code Java:
package monthypayment2;
import java.text.NumberFormat;
import java.util.Scanner;
public class Monthypayment2 {
private static double monthlypay1;
private static String choice;
private static boolean n;
private static String False;
private static boolean y;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//format number and currency
NumberFormat NF = NumberFormat.getCurrencyInstance();
double months;
double numyears;
double loanamount;
double rate;
double monthlypay1;
y = true;
n = false;
//prompt loan amount
System.out.print("Enter Loan Amount:");
loanamount = in.nextDouble();
//prompt rate
System.out.print("Enter Rate:");
rate = in.nextDouble();
//parse these double.parsedouble();
//prompt years
System.out.print("Enter Number of Years:");
numyears = in.nextDouble();
rate = rate / 1200;
months = 12 * numyears;
//calculation monthly
monthlypay1 = loanamount * rate * (Math.pow (1 + rate, months))
/ (Math.pow (1+ rate, months))-1;
int repeat = 1;
while (monthlypay1 > 0) {
System.out.println("The Monthly Payment is: $" + monthlypay1);
break; }
while (monthlypay1 < 0) {
System.out.print("You need to enter positive numerical data!");
break;
}
System.out.print("Would you like to continue calculations(y/n)?");
choice = in.nextLine();
}
}
Ok I used while loops. And fixed the mistakes I've made and got the loop to work. My question that no one has answered however, was
how to loop the user back into the program if they chose the character string "y".
--- Update ---
First off I've tried this. Problem with this is it loops when choosing "n" and doesn't loop when pressing "y" So I decided to start over
for perhaps an easier method.
Code Java:
if (monthlypay1 >= 0) {
System.out.print("The Monthly Payment is: $ " + monthlypay1);
System.out.print("Would you like to calculate again? (Y/N)");
choice = in.next();
while (((Character)'y').toString().equals(choice)); {
main(null);
while (((Character)'n').toString().equals(choice)); {
System.out.print("Thanks for using the program.. Goodbye");
}
}
}
else {
System.out.print("You need to enter Positive Numerical Data!");}
}
}
I am still relatively new to Java. If you can make an example code of how to loop the user back after they choose the string / character that is associated with yes then please do provide it.
Re: If Else loop or do while loop
Quote:
how to loop the user back into the program if they chose the character string "y".
Enclose the program in the loop that is controlled by the user's response of "y"
For example:
Code :
initialize user's answer to "Y"
while(user says "Y") {
do the work here
ask user if he wants to continue
} // end loop
Note: the "ask user..." bit could be inside its own while loop that validates that the user gave a valid answer.
Re: If Else loop or do while loop
Re: If Else loop or do while loop
There is no such thing as an if else loop. If statements and loops are not interchangeable.