Results 1 to 2 of 2
  1. #1
    neveser is offline Member
    Join Date
    Nov 2012
    Posts
    40
    Rep Power
    0

    Default Validate user input (positive and numerical)

    I can't figure out a way to validate user input. The input needs to be both positive and numerical.
    So far the code won't accept any letters, but it still accepts negative numbers.

    Java Code:
    //Get input (minutes and payment) from user
            try{
            minutesIn = Double.parseDouble(minutesField.getText());
            paymentIn = Double.parseDouble(paymentField.getText());
            }
            catch(Exception e)
            {
            return;  
            }
    I am a total noob here and the books I have don't seem to cover this. Actually, both books don't seem to cover much of anything I need.

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,947
    Rep Power
    19

    Default Re: Validate user input (positive and numerical)

    So check the values.
    Create a validate<whatever those values represent>() method that checks them and returns false if they aren't valid, or even throws an exception.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Allow only numerical input on JTextField
    By patriotsfan in forum AWT / Swing
    Replies: 3
    Last Post: 05-31-2011, 12:39 PM
  2. Replies: 2
    Last Post: 05-24-2011, 10:15 AM
  3. Sum positive numbers using 10 inputs from user
    By pvictory1 in forum New To Java
    Replies: 15
    Last Post: 10-10-2010, 01:30 AM
  4. Replies: 2
    Last Post: 05-18-2010, 03:12 PM
  5. How do I validate user input from forms with Java?
    By rickywh in forum New To Java
    Replies: 2
    Last Post: 01-30-2010, 06:49 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
  •