# While loop, help with looping.

• October 29th, 2013, 08:37 AM
Ancharius
While loop, help with looping.
I need help looping my program that I am doing. First off what I am trying to do is prompt the user if they would like to calculate again. If they type in the string "y" then It will loop them back into the beginning. If they type in the string "n" it will break; and the program will be done.

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(); } }```

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.
• October 29th, 2013, 09:33 AM
PhHein
Re: While loop, help with looping.