Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: While loop, help with looping.

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Question 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.

    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.
       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.


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    610
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: While loop, help with looping.

    You already asked that question and you got replies: If Else loop or do while loop

  3. #3
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: While loop, help with looping.

    Sorry. No one in the thread answered my question so I thought I could post a new one. So I suppose a moderator can close this thread.

Similar Threads

  1. DO WHILE LOOPING
    By njabulo ngcobo in forum What's Wrong With My Code?
    Replies: 14
    Last Post: May 22nd, 2013, 09:57 AM
  2. Why is my while loop not looping?
    By kevinsauerzxc in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 24th, 2013, 08:37 PM
  3. Looping Over and Over Again?
    By avalanche72 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 1st, 2012, 04:11 AM
  4. Need help with looping!
    By crsoccerplayer6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 04:09 PM
  5. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM

Tags for this Thread