How do I stop this from looping over and over?

``````import java.text.DecimalFormat;
import java.util.Scanner;

public class Loan1 {

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double interest;
double payment;
double amount;
double term;
DecimalFormat decimalPlaces=new DecimalFormat("0.00");

while (true) { 							//keep looping until we see stop
System.out.println("Enter Interest: ");
interest = input.nextDouble();

term = -1;
while (term <= 0) {
System.out.println("Enter a positive term: "); // prompt
term = input.nextDouble(); // this requires positive number, or keeps asking
}
System.out.print("Type term: "); // prompt

amount = -1;
while (amount <= 0) {
System.out.println("Enter a positive amount: "); // prompt
amount = input.nextDouble(); // this requires positive number, or keeps asking
break;

}

interest = interest/12;//helps simplify the equation
payment = amount*((interest*(Math.pow((interest+1), term))))/(Math.pow((interest+1),term)-1);

input.nextLine();

} // end while
} // end main

}// endloan1``````

You need to add some stop condition for the while(true) loop.

can you give me an example, I am not sure how to do that.

Under what condition do you want to exit the loop? You already have a case in your code in which you terminate an inner loop (line 36). If you are only expecting to have it run once, then there is no need to have a loop.

Otherwise maybe its a question to the user such as "Enter Interest (q to quit)".

Then your loop can look like:

``````String interest = input.next();
while(interest.equalsIgnoreCase("q") == false)
{
... loop code here

interest = input.next();
}``````

Then you just need to parse the number out of the String and use that as the interest rate.

I only need it to run once and then quit, i do not need it to keep going

Drop the loop then, there will be no point for it. Think of loops as a way of executing a piece or chunk of code more then one time.

Also you other loop only executes one time (since you put a break statement there).

hm.
boolean valid = true;
while(valid){
...
body
...
valid = false;
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.