Number Input Validation

I'm having a lot of trouble validating that only a number within the specified range can be taken as input in this program that calculates payments on a loan. The loops repeat and prompt for correct input IF the values inputted are numbers. I need the program to account for Strings and characters, so that if a string or char is entered in the loan amount, year amount, or interest rate amount, the user is prompted again until a number with no chars or strings present is input. I tried everything; I've changed all of my methods to strings and tried to parse the strings into integers, a waste of about 2 hours; I've tried using exception handling, I cannot get it to work right and there's too much overhead for that anyway. If there was just a static method such as NotANumber (NaN) for primitive types so that I could validate at the same time as I take the <variable>.nextDouble, I wouldn't have a problem. Any help please?
public class Payment
    private double la;
    private double mir;
    private int yrs;
    private double monthlyPayment;
    Scanner inp = new Scanner(System.in);
    public Payment()     //no argument constructor
        la =  0;
        mir = 0;
        yrs = 0;
    public static double monthlyPayment(double loanAmount, double monthlyInterestRate, int years)
        int months = 12;// *= years;
        double temp = monthlyInterestRate;
        months *= years;
        monthlyInterestRate = monthlyInterestRate * .01;
        monthlyInterestRate /= 12;
        double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1/Math.pow(1 + monthlyInterestRate, months));
        System.out.println("Loan amount: " + loanAmount);
        System.out.println("Yearly interest rate: " + temp);
        System.out.println("Years: " + years);
        System.out.println("Monthly payment: " + monthlyPayment);
        return 1;
    public void input()
        char c = ' ';
        String s = "";
        double loan;
        double monthlyInterestRate;
        int years;
        Scanner in = new Scanner(System.in);
        do
            do
                System.out.println("Enter loan amount: ");
                loan = in.nextDouble();
            while(loanAmountFalse(loan));
            setLoanAmount(loan);
            do
                System.out.print("Enter monthly interest rate: ");
                monthlyInterestRate = in.nextDouble();
            while(monthlyInterestRateFalse(monthlyInterestRate));
            setMonthlyInterestRate(monthlyInterestRate);
            do
                System.out.print("Enter years: ");
                years = in.nextInt();
            while(yearsFalse(years));
            setYears(years);
            monthlyPayment(loan, monthlyInterestRate, years);
        while(continueOK());       
    private boolean continueOK()
        boolean cont = true;
        String s = "";
        char c = ' ';
        Scanner in = new Scanner(System.in);
        do                                   //allows prompt to repeat if 'y' or 'n' is not entered
            System.out.print("Enter 'y' to continue or 'n' to end program:");
            s = in.next();
            s = s.toLowerCase();            //input is not case sensitive
            c = s.charAt(0);
            if(c == 'n')
                System.out.println("System exiting...");
                System.exit(0);
        while(c != 'y');
        return true;
    private boolean loanAmountFalse(double loanAmount)
        if(loanAmount < 0)
            System.out.println("Loan amount UNDER 0... error...");
            return true;
        else if(loanAmount > 1000000)
            System.out.println("loan amount OVER 1000000...");
            return true;
        else
            System.out.println("loanAmOK method reached...");
            return false;
    private boolean monthlyInterestRateFalse(double monthlyInterestRate)
        if(monthlyInterestRate < 0)
            System.out.println("ERROR: Interest rate must be over 0...");
            return true;
        else if(monthlyInterestRate > 20)
            System.out.println("Error: Interest rate must be under 20...");
            return true;
        else
            System.out.println("good to go...");
            return false;
    private boolean yearsFalse(int years)
        if(years < 0)
            System.out.println("ERROR: years must be over 0...");
            return true;
        else if(years > 100)
            System.out.println("ERROR: years must be under 100...");
            return true;
        else
            System.out.println("Years ok");
            return false;
    public void setLoanAmount(double loanAmount)
        if(loanAmountFalse(loanAmount))
            System.out.print("Error: loan amount must be greater than 0 and less than $1,000,000.");
        else
            this.la = loanAmount;
    public void setMonthlyInterestRate(double monthlyInterestRate)
        if(monthlyInterestRateFalse(monthlyInterestRate))
            System.out.println("Error: interest rate must be greater than 0 and less than 20.");
        else
            this.mir = monthlyInterestRate;
    public void setYears(int years)
        if(yearsFalse(years))
            System.out.println("Error: years must be greater than 0 and less than 100.");
        else
            this.yrs = years;
}

flounder wrote:
Or wrap a try statement around the call to nextDouble.Or use Scanner.hasNextDouble() to determine if the next token can successfully be parsed as a double.

Similar Messages

  • When I input my serial number to activate Photoshop CS4, it said the number is invalid. However, the customer service of Adobe told me that the number is valid. What should I do?

    When I input my serial number to activate Photoshop CS4, it said the number is invalid. However, the customer service of Adobe told me that the number is valid. What should I do?

    Error "The serial number is not valid for this product" | Creative Suite

  • Logic 9 Serial Number Not Valid

    I recently backed up my computer and did a complete OS reinstall (starting from stratch and not using the old image due to old corruption files). I then decided to manualy copy over Logic Pro 9 files from the backup drive to the new image. I went to open Logic and it asked for my serial number. I typed it in and then it decided to crash on me. Well no big deal, I probably didn't copy all the files. So I deleted as many files as I could find and then proceded to reinstalling Logic 9 from my Logic Studio disc set. Entered name and the serial key during the beggining of the install and it says "Serial Number Not Valid" and asked me to type it again and gives me the same message. After 3 failed attempts, it closes the installation program. I have tripple checked my spelling in the key and have checked forums regarding this problem. I have already attempted to move the ID files in Library/Application Support/ProApps to the desktop; no change. Any input would be nice. Thank you.
    I am running Mavericks 10.9.1 on a MacBook pro 2012.

    Was your copy of Logic Studio 2.0 (the one containing LP9) a full version  or an upgrade version?
    If it was an upgrade version you need to enter your original LP8 serial first.... and then it will ask for your LP9 upgrade serial...
    If it was the full version, contact Apple directly and ask for their assistance.
    http://www.apple.com/contact/
    or use the online webpage to obtain a replacement serial number...
    http://support.apple.com/kb/HT1861?viewlocale=en_US&locale=en_US
    Note: Once you have successfully installed LP9.. you must upgrade LP9 to the latest version 9.1.8 before you run it, via software updates... as you are running Mavericks...

  • ID number for validation

    My ID is declared ok by Adobe. But when I try to use it for validation of my Acrobat XI Pro, it says that there is no an eligible software on my computer !!! Amazing ! Have you any idea about that bad teasing problem ?

    My ID is declared Ok by Adobe for this software (Acrobat XI Pro). It also
    can be seen in my personnal count on Adobe site. The problem: Adobe
    applications Manager seems to ignore the software which is on my computer.
    Please also note that it is not an upgrade, but a new load of acrobatXI pro
    after a fatal system crash (Mac OS 10.8.3).
    Thanks for your help
    jjh
    De :  "Bill@VT" <[email protected]>
    Répondre à :  <[email protected]>
    Date :  samedi 6 avril 2013 21:10
    À :  Jean-Jacques Hanet <[email protected]>
    Objet :  ID number for validation
    Re: ID number for validation
    created by Bill@VT <http://forums.adobe.com/people/Bill%40VT>  in Acrobat
    Installation & Update Issues - View the full discussion
    <http://forums.adobe.com/message/5212432#5212432>
    You need to enter the ID for the product you are upgrading from. If you did
    not have a previous version of Acrobat installed, then the Upgrade will not
    install -- you have to purchase the full product. This upgrade aspect is also
    a good reason to not get rid of the previous version and lose the information
    if you ever need to reinstall.
    Please note that the Adobe Forums do not accept email attachments. If you want
    to embed a screen image in your message please visit the thread in the forum
    to embed the image at http://forums.adobe.com/message/5212432#5212432 Replies
    to this message go to everyone subscribed to this thread, not directly to the
    person who posted the message. To post a reply, either reply to this email or
    visit the message page: http://forums.adobe.com/message/5212432#5212432 To
    unsubscribe from this thread, please visit the message page at
    http://forums.adobe.com/message/5212432#5212432. In the Actions box on the
    right, click the Stop Email Notifications link. Start a new discussion in
    Acrobat Installation & Update Issues by email
    <mailto:discussions-community-acrobat-installation_and_update_installation@mai
    l.forums.adobe.com>  or at Adobe Community
    <http://forums.adobe.com/choose-container!input.jspa?contentType=1&containerTy
    pe=14&container=4714>  For more information about maintaining your forum email
    notifications please go to http://forums.adobe.com/message/2936746#2936746.

  • I bought acrobat adobe professtional version II in 2012.  Now I re-download this software to my new computer.  All info was downloaded.  But when I oopened the file, I need to verify the serial number.  It showed up that the serial number is valid, but th

    I bought acrobat adobe professtional version II in 2012.  Now I re-download this software to my new computer.  All info was downloaded.  But when I opened the file, I need to verify the serial number.  I entered.  But it showed up that the serial number is valid, but the qualifying product could not be found.  I could not be able to find Ver 11 from the list.  What can I do?

    The qualifying product is the older version that you owned before you purchased the upgrade to version.  It is what qualifies you to use the less expensive upgrade version.  So you need to select the older version and provide its serial number to prove that you qualify for using the upgrade.
    Error "This serial number is not for a qualifying product" | CS6, CS5.5, CS5
    http://helpx.adobe.com/creative-suite/kb/error-serial-number-qualifying-product.html

  • Keep getting an error message when trying to send some texts to valid numbers, "Error Invalid Number. Please re-send using a valid 10 digit mobile number or valid short code.

    Does anyone know why I keep getting an error  message, "Error invalid number. Please re-send using a valid 10 digit mobile number or valid short code.  The numbers I'm texting to, are valid numbers.

    Did you ever get resolution to this problem?
    My coworker has the identical issue, including the number (+1 (1)(216)116-11) in the error reply.

  • Serial Number Not Valid- new Windows 7pro/ fresh install of Creative Suite 5 Design Premium Win from download will not accept license key. This serial number is not valid for this product is the only response.

    Serial Number Not Valid- new Windows 7pro workstation / fresh install of Creative Suite 5 Design Premium Win from download will not accept license key to install. This serial number is not valid for this product is the only response being returned. What am I missing?

    The key is for Creative Suite 5 Design Premium the download is DesignPremium_CS5_5_LS1 and all associated files in the group.

  • Input validation in an OO-friendly fashion

    Okay, so "input validation" may sound a bit like an issue for the security division of the forum, but I'm concerned with the most OO way to verify the inputs of users. The situation is that I have about five or six different blanks in a GUI (not that it matters) where a user can input something to change the GUI's model. The GUI's model is, itself, an interface and therefore supports pluggability.
    Now, each of the inputs has potentially different "policies" or business rules for what can be entered. For example, one may be a date, etc. You guys know what I mean. Anyway, I know that I could just have some validation method for every field, but this doesn't seem to be very OO-esque.
    I'd like to have some kind of an interface called "StringValidator" or something that would have a method "validate()," but I don't know how this would play out. Maybe I could have a map in my GUI model where the key is the field name and the associated value is an appropriate implementation of StringValidator? I don't know, though. Even though it would be easier to implement five specific methods, one for each field, I'd rather not.
    If I use some kind of StringValidator, maybe I could require a method that returns an array of Strings with all illegal forms, but then again, that's bad form...
    I really just don't know.
    So, in summary, my two questions are as follows:
    1. What is the best way to implement an OO, easily scalable way to validate Strings in my app?
    2. How do I protect against things like SQL statements, HTML scripts, etc.? I know I should (in many cases), but I don't know how to.
    Thanks for any help, and for wading through that description.
    theAmerican
    PS I just remembered something about some Scanner class or something. Maybe that would help?

    It depends quite what you're doing; if you're working with a db then using prepared statements would handle all the escaping for you, but if you allow the users to input raw SQL then you can't realistically stop them from screwing things up.
    Similarly if you're having the users create html somewhere, then the more flexibility there is the harder it is to control. You don't have to worry about scripting if you're just using JLabel to render the html as it doesn't support it; if you're creating a web UI then you may have to.
    Pete

  • I have a new iPhone 5 and receive the following message when texting some people within my contacts.  Message sent using invalid number of digits please resend using 10 digit number or valid short code Msg:2114

    I have a new iPhone 5 and receive the following message when texting some people within my contacts.  Message sent using invalid number of digits please resend using 10 digit number or valid short code Msg:2114. Online I found a discussion that stated i needed to delete contact and re-add. I have done that to no avail. Any ideas?

    I had the same issue with the iPhone 6 and tried everything!  I now understand that at some point I stored a number for a contact without including the area code and sent a text message.  So, to correct the issue, you must literally "erase" all references to the contact, not just the number.  Deleting the contact and entering a new one is not enough because the iPhone stores references to that number in multiple places.  The steps below worked and are very simple:
    1) On the contact screen, delete everything stored for that number.  You can't just edit the contact to fix the problem, so write down or record the info somewhere before you complete this step.  Choose edit and then select DELETE to eliminate the entire contact which means home, work, cell, email address, etc.
    2) Delete the entire string of messages you sent to that number.  That's every single message...
    3) Now delete the entire string of invalid 2114 messages you received after your text(s) failed.
    4) Finaly, remove the contact from FAVORITES if you saved it that way. 
    5) To validate that all this worked, simply go to the keypad or contact screen and enter the number associated with the contact to verify successful removal.  I used the keypad and dialed to confirm.  If the contact number still shows up, you need to go back and recheck your steps.  Trust me, I had to do this a few times.
    Several posts suggested that you should  reset and turn off your phone.  After following these steps, it was not necessary for me to reset or turn off my device. 
    is there a much faster way to achieve the same results?  Probably, but this was safe for an iPhone novice and may help you avoid inadvertently deleting other contacts or replacing factory settings that you need.  

  • MM41/MM42/(MM43) - Sales view: How to add own input validation for CALP-END

    Hello.
    I am looking for an easy way, if any to create an own input validation for a certain field in the article master on the sales view tab. In addition to any standard input validation I would a like to add an own validation (for CALP-ENDPR) depending on the input.
    How can that be achieved in the easiest and proper manner - in general and for the specific case?
    There are no screen exits etc. here, if I am correct.
    Any ideas?
    Thanks.
    C.N.

    Hi,
    Please refer the below link.
    This is for MM01. I understand that you are into Retail system. Hope the same processing logic can be done in your scenario also.
    saptechnical(dot)com(slash)Tutorials(slash)ExitsBADIs(slash)MM(slash)MM01(dot)htm
    Replace the bracket words with the correct symbols.
    With Regards,
    Sumodh.P

  • I try to access my new Adobe Pro XI download to use it, and a message says "serial number not valid" or words to that effect.  I downloaded on Friday afternoon.

    I try to access my new Adobe Pro XI download to use it, and a message says "serial number not valid" or words to that effect.  I downloaded on Friday afternoon.  Why?  This should have been easy.

    Contact support by web chat to get your serial number issues sorted.
    Mylenium

  • Input validations using bsp code

    hai all,
       i want to check user input whether he/she entered correct values r not, i know how to do using javascript.
    but i need to do the same without using javascript.. is there any way
    leoiz

    No, this was not a joke, but it would be possible theoretically.
    Doing a quick search on Google got me this nice link:
    http://www.permadi.com/tutorial/flashjscommand/
    It shows an example of Flash interacting with JavaScript, hence proving the possibility.
    As for really using Flash/ActionScript for input validation ...
    If you are thinking about just including a little Flash-Validation for input fields - why do it with Flash if you can use JavaScript?
    And if your page is a Flash-Page anyway, well, you would not have to go back to HTML input fields, as you work within your Flash applet.
    Maybe you have a specific situation I didn't think of yet.
    Max

  • Message sent using invalid number of digits. Please resend using 10 digit number or valid short code. Msg 2114

    Message sent using invalid number of digits. Please resend using 10 digit number or valid short code. Msg 2114
    How can this be fixed? Most of my numbers are saved as 7 digit

    So I THINK I've figured this out. Hopefully. I was getting the same problem when I was trying to text my boss and my dad. I went back to try to delete the contact and try again and I noticed that I had the little phone icon next to their numbers and not the messaging text bubble. But I'd also left their type of phone as "home" in the drop down menu. I edited that to mobile and boom, got the text messaging bubble. Tried texting them both, I could now send the message.
    I think if the number is set to home the phone assumes it's a landline and doesn't even try to send. I think the people who have had success deleting and re-inserting the contact edited that piece of information without realizing it.
    I really hope this solves your problem!

  • How do I return to the top of a form when input validation fails?

    I have a form that I am using spry input validation that I
    would like the user to be returned to the top of the page when
    validation fails. If that can't be done somewhat easily, can I have
    a message appear next to the submit button that says "Errors found.
    The field(s) marked in red need to be corrected" when there are any
    errors that prevent the form from submitting.

    The break statement in Java is similar to last in Perl.
    The continue statement in Java is similar to next in Perl.

  • I just downloaded photoshop elements 7.0 to a new box and it tells me that my serial number is not valid.  the adobe support team says the number is valid, but that they can't help me

    i just downloaded photoshop elements 7.0 to a new box and it tells me that my serial number is not valid. the adobe support team says the number is valid, but that they can't help me

    Which operating system are you using?
    When you say downloaded, where did download pse 7 from?
    Do you have the original install disk or original download for pse 7?
    I believe the serials won't interchange between the disk version of pse 7 and the electronic down load version.

Maybe you are looking for

  • Error while running UTL_MAIL package

    SQL> conn system/sys@ORCL; Connected. SQL> @F:\oracle\product\10.2.0\client_5\RDBMS\ADMIN\utlmail.sql Package created. Synonym created. SQL> @F:\oracle\product\10.2.0\client_5\RDBMS\ADMIN\prvtmail.plb Warning: Package Body created with compilation er

  • My websites and UI have suddenly become huge. How can I get them back to a normal, smaller size?

    Yesterday all of a sudden my webpages and UI appeared to be magnified. While I can zoom out on websites, I cannot find any option to adjust the size of the UI. I noticed that Firefox 22 (which I am running) has a new feature: "Windows: Firefox now fo

  • Pantone 2035C not found in the Adobe Programms

    Good Morning, We choose the Pantone 2035C (336NEW from the Formula Guide Solid Coated) but unfortunately, we cannot activate this color in the Adobe Programm (Illustrator and Photoshop). When is it planed to integrate these news colors as a standard

  • Logitech Cordless Optical Mouse doesnt work with mac os x 10.4.5?

    I just bought a Logitech® Cordless Optical Mouse in japan to use with my powerbook running os x 10.4.5...the mouse doesnt seem to work with the native bluetooth system. so I plugged in the supplied reciever and could get the mouse to move the cursor,

  • Infotype Update gives error

    Hi, I tried to update infotype 0000 with HR_INFOTYPE_OPERATION. It gave me a error with name  ASSIGN_BASE_TOO_SHORT Following is the error analysis. Pls guide me. Error analysis     The source field is too short.     In the running program "SAPLHRMM"