Results 1 to 2 of 2
  1. #1
    nemo is offline Member
    Join Date
    May 2007
    Rep Power

    Default How to determine if the value of a JTextField is double

    I have a textfield and i want it to have only double values. How can i make sure of this?

  2. #2
    levent Guest


    Double has parseDouble method which tries to parse a String as a Double. You need to put it inside a try-catch block since it throws NumberFormatException.

    You can write logic inside catch since it is called if the value is not a valid double!

    Here is an example code:

    Java Code:
    double value;
    try {
      // String textValue = the string from your JTextField
      value = Double.parseDouble(textValue);
      // if the code gets to here, it was recognizable as a double
    catch(NumberFormatException e) {
      // if the code gets to here, it was NOT recognizable as a double

Similar Threads

  1. How to determine CPU usage using Java?????
    By JavaEmpires in forum New To Java
    Replies: 5
    Last Post: 03-16-2011, 04:49 AM
  2. how to focus to another JTextfield?
    By birdofprey in forum AWT / Swing
    Replies: 2
    Last Post: 04-09-2008, 01:08 PM
  3. Replies: 3
    Last Post: 01-19-2008, 06:05 AM
  4. how to determine the primary key
    By osval in forum JDBC
    Replies: 1
    Last Post: 08-07-2007, 02:31 AM
  5. help with JTextfield
    By gary in forum New To Java
    Replies: 4
    Last Post: 07-11-2007, 01:58 PM

Posting Permissions

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