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.
Printable View
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.
Call one of the parse... methods in a try{}catch block and let the parse method throw an exception if the number is invalid.
I'm sori.. whats parse... methods ..
try{
float f = Float.parseFloat(String);
}
catch(NumberFormatException nfe){
jOptionPane()....
}
am I right??
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.Quote:
particulars Float.parseFloat(String)
n e way.. thnx for confirmation.
however i wanted to use it within if then else statements.. how can i do that???
I guess the execution of the program stops after an exception is caught.. am i right??
and what about the code after the catch block..
try{
}catch(){
}
statments..
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.
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
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.
I am sori.. i didn't get it. Can you please elaborate..
If you're getting user input from the command line, not from a GUI, you could do something like (pseudocode)
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
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.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) exceeded
exactly thats what i am asking..in this post. How can i implement.. userInputBad is true
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.
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. thnxCode: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);
}
}