Page 1 of 2 12 LastLast
Results 1 to 20 of 27
  1. #1
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default if it is floating point number

    Hi.. I'm fetching the data from user as string.. I want to confirm that the entered string.. in particulars Float.parseFloat(String) is a floating point number.. I guess i need to do exception handling.. please suggest.
    The Quieter you become the more you are able to hear !

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    Call one of the parse... methods in a try{}catch block and let the parse method throw an exception if the number is invalid.

  3. #3
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    I'm sori.. whats parse... methods ..
    The Quieter you become the more you are able to hear !

  4. #4
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    try{
    float f = Float.parseFloat(String);
    }
    catch(NumberFormatException nfe){
    jOptionPane()....
    }

    am I right??
    The Quieter you become the more you are able to hear !

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    That's a lazy way of saying a method the begins with parse and ends with either Long or Double or ...

    You had posted this, so I figured you knew.
    particulars Float.parseFloat(String)
    Yes, that looks about right.

  6. #6
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    n e way.. thnx for confirmation.
    The Quieter you become the more you are able to hear !

  7. #7
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    however i wanted to use it within if then else statements.. how can i do that???
    The Quieter you become the more you are able to hear !

  8. #8
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    I guess the execution of the program stops after an exception is caught.. am i right??
    The Quieter you become the more you are able to hear !

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Stephen Douglas View Post
    I guess the execution of the program stops after an exception is caught.. am i right??
    If an exception occurs within the try block that is being caught by the catch block, then yes, the execution within the try block stops and the code in the catch block starts executing.

  10. #10
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    and what about the code after the catch block..

    try{
    }catch(){
    }
    statments..
    The Quieter you become the more you are able to hear !

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The code flow will resume after the catch block unless you tell it to do something else from within the catch block. For instance in one of my programs if a critical process fails in the try block, I have a System.exit(some number) in the catch block -- after printing the exceptions stacktrace of course! In another, if the program detects that the user as entered invalid input, the catch block will clear the JTextFields and show a JOptionPane warning the user of the error and prompting them for more correct input, but the program doesn't exit. It's your choice.

  12. #12
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    try {
    preparedStatement = connection.prepareStatement("update rms_ratestore set ratesperkg = ? where description = ?");
    preparedStatement.setString(1, _data.toString());
    float f = Float.parseFloat(rSet.getObject(1).toString());
    preparedStatement.setString(2, rSet.getObject(1).toString());
    } catch (NumberFormatException ne) {
    JOptionPane.showMessageDialog(null, "Invalid number format", "", JOptionPane.ERROR_MESSAGE);

    }
    preparedStatement.executeUpdate();
    }

    In my case red part is executed unwillingly.. in case the user enters the incorrect floating point number i want the application to prompt the user to enter the correct value before attempting to update the tables..

    so i was wondering if i could simply use the if then else.. such as if.. incorrect input jOptionPane.. or else update the table..
    please suggest.thnx
    The Quieter you become the more you are able to hear !

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Please don't forget code tags.

    You could use a while loop and set a control variable at the bottom of the try block. If the code flow reaches that point, you know that all has worked OK, and then set the while boolean control variable to false so that the loop ends.

  14. #14
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    I am sori.. i didn't get it. Can you please elaborate..
    The Quieter you become the more you are able to hear !

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you're getting user input from the command line, not from a GUI, you could do something like (pseudocode)

    Java Code:
    boolean userInputBad is true
    while userInputBad
      get user input
      try block
        use user input
        if we've made it here, no exceptions thrown.  Change userInputBad to false
      catch
        warn the user that user input is no good and ask for more input.
    end while

  16. #16
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    Java Code:
                            try {
                                preparedStatement = connection.prepareStatement("update rms_ratestore set ratesperkg = ? where description = ?");
                                preparedStatement.setString(1, _data.toString());
                                float f = Float.parseFloat(rSet.getObject(1).toString());
                                preparedStatement.setString(2, rSet.getObject(1).toString());
                            } catch (NumberFormatException ne) {
                               [COLOR="Red"] _model.setValueAt(rSet.getObject(2).toString(), row, column);[/COLOR]
                                JOptionPane.showMessageDialog(null, "Invalid number format", "", JOptionPane.ERROR_MESSAGE);
                            }
    In this code.. actually i have fetched data from a table in the database.. now using tablemodellistener i allow the user to edit the values in the table.

    the purpose of the red portion is that .. suppose initially the position 1,1 holds the value "120" now user tries to edit it as "120ejdgs".. so a popup message is displayed.. now i want that when the user clicks ok the jOptionPane. the 1,1 is reset to "120".. but the following error is displayed..
    java.sql.SQLException: ORA-00020: maximum number of processes (150) exceeded
    The Quieter you become the more you are able to hear !

  17. #17
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    exactly thats what i am asking..in this post. How can i implement.. userInputBad is true
    The Quieter you become the more you are able to hear !

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm not able to see what's wrong given the information at hand. You seem to be catching an error in the result set information (don't see where you obtain the result set) but not in the data entered into the JTable which confuses me.

    You need to study the line that causes the exception and you may wish to create an SSCCE.

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by Stephen Douglas View Post
    exactly thats what i am asking..in this post. How can i implement.. userInputBad is true
    Since we're talking a GUI, then you can't use a while loop, but will need to use other logic depending on the GUI structure.

  20. #20
    Stephen Douglas's Avatar
    Stephen Douglas is offline Senior Member
    Join Date
    Mar 2010
    Posts
    137
    Rep Power
    0

    Default

    Java Code:
    import javax.swing.*;
    import javax.swing.table.*;
    import javax.swing.event.*;
    
    public class table extends javax.swing.JFrame implements TableModelListener {
    
        String data[][] = {};
        String col[] = {"DESCRIPTION", "RATES/KG"};
        DefaultTableModel model = new DefaultTableModel(data, col);
        protected boolean flag = false;
        protected String str = "120";
    
        public table() {
            initComponents();
            jTable1.getModel().addTableModelListener(this);
            Object[] rowData = {"100", "120"};
            model.addRow(rowData);
            flag = true;
        }
    
        public void tableChanged(TableModelEvent e) {
            if (flag) {
                int row = e.getFirstRow();
                int column = e.getColumn();
                TableModel _model = (TableModel) e.getSource();
                Object _data = _model.getValueAt(row, column);
                if (_data != null) {
                    if (column == 1) {
                        try {
                            float f = Float.parseFloat(_data.toString());
                        } catch (NumberFormatException ne) {
                            [COLOR="Red"]_model.setValueAt(str, row, column);[/COLOR]
                            JOptionPane.showMessageDialog(null, "Invalid number format", "", JOptionPane.ERROR_MESSAGE);
                        }
                    }
                } else {
                    JOptionPane.showMessageDialog(null, "Null entries are not allowed !", "", JOptionPane.INFORMATION_MESSAGE);
                }
            }
        }
    
        @SuppressWarnings("unchecked")
        // <editor-fold defaultstate="collapsed" desc="Generated Code">
        private void initComponents() {
    
            jScrollPane1 = new javax.swing.JScrollPane();
            jTable1 = new javax.swing.JTable() {
    
                boolean[] canEdit = new boolean[]{
                    true, true
                };
    
                @Override
                public boolean isCellEditable(int rowIndex, int colIndex) {
                    return canEdit[colIndex];
                }
            };
    
            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setBackground(new java.awt.Color(255, 255, 255));
            setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    
            jScrollPane1.setBackground(new java.awt.Color(255, 255, 255));
            jScrollPane1.setFont(new java.awt.Font("Times New Roman", 0, 11));
    
            jTable1.setFont(new java.awt.Font("Times New Roman", 0, 11)); // NOI18N
            jTable1.setModel(model);
            jTable1.setRowHeight(20);
            jScrollPane1.setViewportView(jTable1);
    
            getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);
    
            pack();
        }// </editor-fold>
        private javax.swing.JScrollPane jScrollPane1;
        private javax.swing.JTable jTable1;
    
        public static void main(String args[]) {
            table t = new table();
            t.setBounds(200, 200, 500, 500);
            t.setVisible(true);
        }
    }
    I have prepared my SSCCE but it seems to be no problem with the code in red.. what could possibly be the reason for the problem.. please suggest. thnx
    The Quieter you become the more you are able to hear !

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: 07-14-2009, 07:04 PM
  2. Replies: 2
    Last Post: 04-01-2009, 08:37 AM
  3. java floating point comparison
    By sardare in forum Advanced Java
    Replies: 6
    Last Post: 03-03-2009, 04:11 PM
  4. number of floating point
    By mohammad8065 in forum Advanced Java
    Replies: 5
    Last Post: 12-28-2008, 09:41 AM
  5. Floating point values in SWT Spinner
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:50 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
  •