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

Thread: If Else loop or do while loop

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

    Post 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:
    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);
     
     
        }
    }


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

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

  3. #3
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    563
    My Mood
    Sleepy
    Thanks
    9
    Thanked 82 Times in 76 Posts

    Default Re: If Else loop or do while loop

    Ok pseudocode:
    do
      get user input
      if user input not ok
        display error message
    while user input not ok

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

    Default Re: If Else loop or do while loop

    I put the statements in the incorrect place and tried this.
     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?

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,758
    Thanks
    45
    Thanked 1,975 Times in 1,949 Posts

    Default Re: If Else loop or do while loop

    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 {
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    26
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

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

    Default Re: If Else loop or do while loop

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

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,758
    Thanks
    45
    Thanked 1,975 Times in 1,949 Posts

    Default Re: If Else loop or do while loop

    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:
    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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. The Following User Says Thank You to Norm For This Useful Post:

    Ancharius (October 29th, 2013)

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

    Default Re: If Else loop or do while loop

    Thanks a lot!

  11. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,465
    My Mood
    Grumpy
    Thanks
    0
    Thanked 158 Times in 149 Posts

    Default 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.
    Improving the world one idiot at a time!

Similar Threads

  1. help with when the for loop is met and i want to run the while loop again
    By m49er704 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 22nd, 2013, 09:03 AM
  2. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 10:22 PM
  4. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 07:57 PM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM

Tags for this Thread