Results 1 to 4 of 4
  1. #1
    shinju is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Loops validation not woorking - help

    I'm trying to do a nested if inside a loop. The user has to enter a period between 5, 10, 15, 20, 25 years. Each year has a different rate and if the user enter a number that is not within these numbers the question is ask again.

    The program runs good but the nested do-while loop is not working. If you see the condition of the while then you can see that it have to be a validation and if it is not in the range of those numbers it has to ask the questions again. And if you type 0 it ends the progam. The validation it's not working and I don't not why

    import java.util.Scanner;
    import java.text.DecimalFormat;
    public class ACMEMORTGAGE
    {
    public static void main (String args [])

    {
    //Declare variables
    double principal, rate=0;
    int mortgageTerm;

    Scanner key=new Scanner(System.in);

    DecimalFormat decimalPlaces=new DecimalFormat("$0.00");
    do
    {
    System.out.print("Enter principal amount (0 to end program):");
    principal=key.nextInt();

    do

    {
    System.out.print("Enter mortgage amortization (1, 2, 3, 5, 10.):");
    mortgageTerm=key.nextInt();

    if (mortgageTerm==1)
    {
    rate=0.035;
    }
    else if (mortgageTerm==2)
    {
    rate=0.039;
    }
    else if (mortgageTerm==3)
    {
    rate=0.044;
    }
    else if (mortgageTerm==5)
    {
    rate=0.05;
    }
    else if (mortgageTerm==10)
    {
    rate=0.060;
    }


    } while (mortgageTerm==1 && mortgageTerm==2 && mortgageTerm==3 && mortgageTerm==5 && mortgageTerm==10);




    }while (principal!=0);

    }

    }

  2. #2
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default Re: Loops validation not woorking - help

    while (mortgageTerm==1 && mortgageTerm==2 && mortgageTerm==3 && mortgageTerm==5 && mortgageTerm==10);
    this is why. you are checking if mortgageTerm equals 1 AND 2 AND 3 AND 5 AND 10..
    which will never happen.
    try using || (or).

  3. #3
    shinju is offline Member
    Join Date
    Oct 2012
    Posts
    4
    Rep Power
    0

    Default Re: Loops validation not woorking - help

    Thanks,

    I tried that but is still not working

  4. #4
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: Loops validation not woorking - help

    There is a couple of key things wrong here. For one, think about what your middle loop is doing. What you are originally saying is "While variable is equal to all these numbers, continue to do this"; which is impossible. The 'or's would make is say "While variable is equal to any of these numbers, do this". What you want it to say is "While variable is NOT equal to all these numbers, continue to do this". So you were right in using the 'and's, but you need to make it '!=' to all the numbers.

    The second key thing that is wrong here is the quit option. So the user enters 0 to quit. What happens? They still must go through the middle loop before they are allowed out of the program. Due to this, the middle loop should be a while loop. What you want it to say is "While variable is NOT equal to all these numbers AND the principal is not equal to 0, continue to do this". This will allow the user to exit the program right after they select 0. Due to this, you will probably have to declare the mortgageTerm right before the middle loop as an invalid number; such as 4. As soon as the user enters a number, the invalid number will be replaced by the user input; hopefully a valid number.

    Also, you should put an ending 'else' to your if statement. If you are going to repeat something until the user gets it right, your should notify the user when they enter an invalid number. Something like:
    Java Code:
    else
      System.out.println("Invalid Input!!!");

    Also in the future, wrap your code up with [code] ...code... [/code] tags. I glanced at this earlier, but was unwilling to read through it at first due to it being unformatted. You will get much more help here if you do this. Just an FYI thing.


    Also, I know that everyone has different formatting techniques. I perfer:
    Java Code:
    else{
      //code
    }
    Others perfer:
    Java Code:
    else
    {
      //code
    }
    But nobody perfers:
    Java Code:
    else
    
    {
      //code
    }
    So you might want to fix some of your formatting; hopefully it was just caused by a crude copy and paste, but either way it is slightly annoying. After main, and the second 'do while' loop you do this...


    Not trying to be hyper-critical, just another FYI thing. Hope this helps you out.
    Last edited by penguinCoder; 10-15-2012 at 01:14 AM.

Similar Threads

  1. Validation
    By Johnny2009 in forum New To Java
    Replies: 2
    Last Post: 09-24-2012, 12:11 AM
  2. Validation of XML against XSD
    By abhinav_sharma in forum New To Java
    Replies: 0
    Last Post: 05-02-2012, 10:18 AM
  3. XML validation
    By Onra in forum New To Java
    Replies: 0
    Last Post: 03-24-2011, 07:14 PM
  4. Replies: 20
    Last Post: 03-22-2011, 05:02 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •