Results 1 to 20 of 27
Thread: if it is floating point number
- 07-05-2010, 10:21 PM #1
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 !
- 07-05-2010, 10:25 PM #2
Call one of the parse... methods in a try{}catch block and let the parse method throw an exception if the number is invalid.
- 07-05-2010, 10:29 PM #3
I'm sori.. whats parse... methods ..
The Quieter you become the more you are able to hear !
- 07-05-2010, 10:31 PM #4
try{
float f = Float.parseFloat(String);
}
catch(NumberFormatException nfe){
jOptionPane()....
}
am I right??The Quieter you become the more you are able to hear !
- 07-05-2010, 10:33 PM #5
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.
Yes, that looks about right.particulars Float.parseFloat(String)
- 07-05-2010, 10:37 PM #6
n e way.. thnx for confirmation.
The Quieter you become the more you are able to hear !
- 07-05-2010, 10:40 PM #7
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 !
- 07-05-2010, 10:41 PM #8
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 !
-
- 07-05-2010, 10:49 PM #10
and what about the code after the catch block..
try{
}catch(){
}
statments..The Quieter you become the more you are able to hear !
-
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.
- 07-05-2010, 11:00 PM #12
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.thnxThe Quieter you become the more you are able to hear !
-
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.
- 07-05-2010, 11:08 PM #14
I am sori.. i didn't get it. Can you please elaborate..
The Quieter you become the more you are able to hear !
-
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
- 07-05-2010, 11:14 PM #16
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.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); }
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) exceededThe Quieter you become the more you are able to hear !
- 07-05-2010, 11:17 PM #17
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 !
-
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.
-
- 07-05-2010, 11:58 PM #20
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. thnxJava 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); } }The Quieter you become the more you are able to hear !
Similar Threads
-
Floating point operations is slower when small values are used?
By Cesaro in forum Advanced JavaReplies: 1Last Post: 07-14-2009, 07:04 PM -
A Polygon class which accepts floating point coordinates
By soorena in forum New To JavaReplies: 2Last Post: 04-01-2009, 08:37 AM -
java floating point comparison
By sardare in forum Advanced JavaReplies: 6Last Post: 03-03-2009, 04:11 PM -
number of floating point
By mohammad8065 in forum Advanced JavaReplies: 5Last Post: 12-28-2008, 09:41 AM -
Floating point values in SWT Spinner
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks