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

Thread: how to check is DOUBLE JTEXTFIELD is empty?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default how to check is DOUBLE JTEXTFIELD is empty?

    hi I've been working this for 2 days and even google it but I can't figured the right answer.
    I can check STRING if it is empty.
    here is my code


     
     private void saveButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                            
            String inventcodef = inventCodeField.getText();
            String inventnamef = inventNameField.getText();
            String categ = cmbname.getSelectedItem().toString();
            double inventreorderf = Double.parseDouble(inventReorderField.getText());
            String inventlocf = inventLocationField.getText();
            String inventstat = inventStatusArea.getText();
     
     
     
     
                if ((inventcodef.trim().length()==0) || (inventnamef.trim().length()== 0)||(inventreorderf == 0))
            {
     
             JOptionPane.showMessageDialog(null,"Item Code / Item Name / ReOrderpoint is empty!");  
     
     
            }
                else
     
            try {
                String SQL2 = "INSERT INTO inventory VALUES ("
                        + "'" + inventcodef + "'" + ","
                        + "'" + inventnamef + "'" + ","
                          + "'" + categ + "'" + ","
                        + "'" + inventreorderf + "'" + ","
                        + "'" + inventlocf + "'" + ","
                        + "'" + inventstat + "'"
                        + ")";
     
                PreparedStatement stamt = connect.prepareStatement(SQL2);
                stamt.executeUpdate();
                Double.parseDouble(inventReorderField.getText());
     
                JOptionPane.showMessageDialog(null,"Item Code: " + inventcodef + " Successfully saved!");
     
            }
              catch (NumberFormatException e)
              {
     
             JOptionPane.showMessageDialog(null,e.getMessage());
             inventReorderField.selectAll();
             inventReorderField.requestFocus();
             return;
     
              }
     
            catch (Exception f)
                {
                 JOptionPane.showMessageDialog(null,(f.getMessage())); 
            }
     
     
     
        }

    Thank you!


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    106
    My Mood
    Yeehaw
    Thanks
    8
    Thanked 11 Times in 11 Posts

    Default Re: how to check is DOUBLE JTEXTFIELD is empty?

    == null;

    ? does that not work?

    I looked using Google and I found this as well,
    jtextfield.getText().trim().length() >=0

    unfortunately I don't know much about GUI form methods, so i don't even know if DOUBLE jtextfield is a special type of jtextfield.
    anyway, maybe the generic ideas help anyway.

    Good-luck,
    Jonathan
    Last edited by JonLane; February 22nd, 2012 at 01:15 AM.

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: how to check is DOUBLE JTEXTFIELD is empty?

    a) You should learn how to use a PreparedStatement correctly (eg using parameters) b) You can use Double.parseDouble and catch any exceptions that may be thrown - alternatively use a regular expressions to check the entered value matches the appropriate pattern

    == null;

    ? does that not work?

    I looked using Google and I found this as well,
    jtextfield.getText().trim().length() >=0

    unfortunately I don't know much about GUI form methods, so i don't even know if DOUBLE jtextfield is a special type of jtextfield.
    I would recommend you become familiar with the API and methods being used prior to trying to make recommendations to others

Similar Threads

  1. How to not accept empty jfield?
    By myexternall11 in forum Java Theory & Questions
    Replies: 1
    Last Post: February 20th, 2012, 04:38 AM
  2. extended JPanel is empty
    By pottsiex5 in forum AWT / Java Swing
    Replies: 4
    Last Post: November 26th, 2011, 03:59 PM
  3. How to check for empty mysql table
    By A4Andy in forum Java Theory & Questions
    Replies: 1
    Last Post: August 26th, 2011, 11:43 PM
  4. [SOLVED] Read double from console without having to read a string and converting it to double.
    By Lord Voldemort in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 26th, 2011, 08:08 AM
  5. Replies: 3
    Last Post: June 11th, 2011, 02:40 PM