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 12 of 12

Thread: Having a bit of trouble trying to loop my project.

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

    Question Having a bit of trouble trying to loop my project.

    public static void main(String[] args) {
            Scanner in = new Scanner(System.in);
     
            //format number and currency
           NumberFormat NF = NumberFormat.getCurrencyInstance();
           NumberFormat NI = NumberFormat.getNumberInstance();
     
     
            double months = 1;
            double numyears;
            double loanamount;
            double rate;
     
     
     
           //put while statement here
     
            //prompt loan amount
            System.out.print("Enter Loan Amount:");                           
            loanamount = in.nextDouble();
     
            //prompt rate
            System.out.print("Enter Rate:");
             rate = in.nextDouble();
     
             //prompt years
            System.out.print("Enter Number of Years:");
            numyears = in.nextDouble();
     
     
          //calculation monthly
           monthlypay1 = loanamount * rate / (Math.pow(1 + rate, months)) / (Math.pow(1 + rate, months)-1);
     
     
            System.out.println("The Monthly Payment is: $" + monthlypay1);
     
     
            if (monthlypay1 < 0) {
                System.out.print("You need to enter positive numerical data!");}
            else {
                System.out.print("Would you like to continue calculations(y/n)?");
            }
                choice = in.next();
                System.out.println();
                n = False;
                if (choice.equals(n)) {
                }
                }
     
     
     
     
            }

    Basically I am trying to loop this. I've tried adding a while statement
     while (n.equals( n) {
    I've also tried while n == true basically trying to make a while statement to loop the whole thing so that at the end if the user selects "Y" it will loop them back. If the user selects "N" it will break them out the loop. Please help.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Having a bit of trouble trying to loop my project.

    I recommend do-while loops in these cases. Are you familiar with those?
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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

    Default Re: Having a bit of trouble trying to loop my project.

    No sir, For the most part i've just been looking at tutorials on websites but they have not been helping much.

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Having a bit of trouble trying to loop my project.

    Ok. Well a do-while loop works similar to a while loop, except the operation of the while loop is guaranteed to run at least once, and the loop continue condition is determined at the end of each iteration (instead of before, like in the while loop).
    Oracle's tutorial can be found here: The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    A simple example of a do-while loop would be like this:
    Scanner in = new Scanner(System.in);
    int repeat = 0; // This will be used in the condition of our loop
    do {
    	System.out.println("Hello");
    	System.out.println("Enter 0 to repeat, or any other number to exit"); // Prompt the user for input
    	repeat = in.nextInt(); // Recieve that input
    } while(repeat==0); // Condition for the loop

    The code in the do block is guarenteed to run at least once. It will only run again if the user enters 0.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Ancharius (October 16th, 2013)

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

    Default Re: Having a bit of trouble trying to loop my project.

    Ok so basically in my case I can change "int repeat = 0" to be repeat = y;
    and while y.equals(true);?

  7. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Having a bit of trouble trying to loop my project.

    That depends on how your input is working. Does the user enter the String y or n? If so, you need to read that in as a String with the Scanner, and do a comparison like: while(input.equals("y"));
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

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

    Default Re: Having a bit of trouble trying to loop my project.

    Ah ok That was going to be my next question. Thanks a lot!

     
    int repeat = 0;
     
      while (monthlypay1 < 0) {
                System.out.print("You need to enter positive numerical data!");}
            while (monthlypay1 > 0) {
                System.out.print("Would you like to continue calculations(y/n)?");
                choice = in.nextLine();
            }
            while (choice.equals("y")) { //insert loop here.}
    while (choice.equal("n")) { break;}
    Something like that?

  9. #8
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Having a bit of trouble trying to loop my project.

    Close, but not exactly. I assume you wanted those whiles to be if/elses. I also assume you wanted all of your code in your do-while loop. For do-while loops, everything you are looping goes inside of the do-block. There actually is not a block after the while loop (it looks really odd, I know).
    String choice = "";
    do {
            //prompt loan amount
            System.out.print("Enter Loan Amount:");                           
            loanamount = in.nextDouble();
     
            //prompt rate
            System.out.print("Enter Rate:");
             rate = in.nextDouble();
     
             //prompt years
            System.out.print("Enter Number of Years:");
            numyears = in.nextDouble();
     
     
          	//calculation monthly
           	monthlypay1 = loanamount * rate / (Math.pow(1 + rate, months)) / (Math.pow(1 + rate, months)-1);
     
     
            System.out.println("The Monthly Payment is: $" + monthlypay1);
     
    	if (monthlypay1 < 0) {
                System.out.print("You need to enter positive numerical data!");}
            else {
                System.out.print("Would you like to continue calculations(y/n)?");
            }
    	choice = in.nextLine();
    } while(choice.equals("y"));
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  10. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Ancharius (October 16th, 2013)

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

    Default Re: Having a bit of trouble trying to loop my project.

    Ok I used the code you had as a guideline .

    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();
           NumberFormat NI = NumberFormat.getNumberInstance();
     
     
            double months = 1;
            double numyears;
            double loanamount;
            double rate;
     
           int repeat = 0;   //condition for loop
     
           do {
     
     
     
            //prompt loan amount
            System.out.print("Enter Loan Amount:");                           
            loanamount = in.nextDouble();
     
            //prompt rate
            System.out.print("Enter Rate:");
             rate = in.nextDouble();
     
             //prompt years
            System.out.print("Enter Number of Years:");
            numyears = in.nextDouble();
     
     
          //calculation monthly
           monthlypay1 = loanamount * rate / (Math.pow(1 + rate, months)) / (Math.pow(1 + rate, months)-1);
     
     
            System.out.println("The Monthly Payment is: $" + monthlypay1);
     
             y = true;
             n = false;
     
            if (monthlypay1 < 0) {
                System.out.print("You need to enter positive numerical data!");}
            else {
                System.out.print("Would you like to continue calculations(y/n)?");
                choice = in.nextLine();
            }
           } while (choice.equals("y"));
     
                    while(choice.equals("n")) {
                break; }
                   }
     
     
                }

    And yea I did wanted if and else statement but one of my friends who tried to help me said I should use do-while loops.

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

    Default Re: Having a bit of trouble trying to loop my project.

    Can anyone help me out with the math on here?
     //calculation monthly
           monthlypay1 = loanamount * rate / (Math.pow(1 + rate, months)) / (Math.pow(1 + rate, months)-1);
    Is what I have.. But i'm not getting the correct numbers...
    When i put in loan amount 100000, with a rate of 6%, and a yearly payment of 30 it should be 599.55$

  13. #11
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Having a bit of trouble trying to loop my project.

    The best advice I could give is to break that down into multiple statements based on order of operations and print out each calculation, so you can see where it goes wrong.

    Break it down to something like this:
    double calc1 = loanamount*rate;
    System.out.println(calc1+"");
    double calc2 = Math.pow(1 + rate, months);
    System.out.println(calc2+"");
    double calc3 = Math.pow(1 + rate, months)-1;
    System.out.println(calc3+"");
    double calc4 = calc1/calc2;
    System.out.println(calc4+"");
    monthlypay1 = calc4/calc3;
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  14. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Ancharius (October 17th, 2013)

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

    Default Re: Having a bit of trouble trying to loop my project.

    I'll try that. Because I noticed every time that I did my math it came out incorrect numbers. When I compare my code to the formula they are the exact same. But maybe that will help Java make the calculations in the correct order. Thanks again!

Similar Threads

  1. Trouble starting java web project
    By MilkWetGhost in forum Java IDEs
    Replies: 2
    Last Post: October 3rd, 2013, 09:17 AM
  2. Trouble with while loop and the use of the Iteration class.
    By simpson_121919 in forum Loops & Control Statements
    Replies: 7
    Last Post: February 17th, 2013, 10:45 PM
  3. Having trouble with for loop statement.
    By Truck35 in forum Loops & Control Statements
    Replies: 3
    Last Post: November 25th, 2012, 04:45 PM
  4. [SOLVED] Trouble with my while loop
    By masterhand89 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 17th, 2012, 12:45 PM
  5. Having Trouble Using a Project as a Library
    By snowguy13 in forum Java IDEs
    Replies: 0
    Last Post: July 5th, 2012, 08:11 PM

Tags for this Thread