Reputation: 1767
I used the below pgm to save the details on address book. I want to check the mob filed whether it is an int or not. It will execute properly at the first time (If i enter letter in mob field it will not save). but from the 2nd time on words it will save even if i entered a letter in the mob filed. mob will store the previous value. How can i clear the mob filed after executing the first time.....
public void savePerson() {
name = jtfName.getText();
name = name.toUpperCase();
jtfName.setText(name);
address = jtfAddress.getText();
try {
landline = Integer.parseInt(""+jtfLandline.getText());
} catch(Exception e) {
}
try {
mob = Integer.parseInt(""+jtfmob.getText());
}catch(Exception e) {
}
email = jtfEmail.getText();
if(name.equals("")) {
JOptionPane.showMessageDialog(null, "Please enter person name.");
} else if(mob == 0) {
JOptionPane.showMessageDialog(null, "Please enter Mobile Number.");
} else {
//create a PersonInfo object and pass it to PersonDAO to save it
PersonInfo person = new PersonInfo(name, address, landline, mob , email);
pDAO.savePerson(person);
JOptionPane.showMessageDialog(null, "Person Saved");
clear();
}
}
Upvotes: 1
Views: 2375
Reputation: 2274
Use the following in your constructor.
jTextField1.setText("");
It will reset your values each time before you click the button.
Upvotes: 0
Reputation: 904
This is very obvious case. When you parse the jtfmob text field first time, It will look for the value in it .. If the value is not integer, it will not be parsed at all and you will get null value. But if the value is integer you would be proceeded without any problem.
mob = Integer.parseInt(""+jtfmob.getText());
mob will contain the parsed value in integer data type . BUT next time when you put letter in text field, it wouldnot be parsable and hence cause an exception (which you are catching successfully).The control of program would be transferred to catch block without updating the value of Integer type variable mob. so mob will contain the OLD value (because value is not updated because of exception) and same you are getting in this case .
To solve this problem either show some message in catch block to enter right value or find some other way by yourself
Upvotes: 1
Reputation: 6565
Try out this,
Once the first iteration is over, reset your text field by setting like below
jtfmob.setText("");
To get the number inputs alone, try the following codes
jtfmob = new JFormattedTextField(NumberFormat.getInstance());
Upvotes: 2
Reputation: 3820
Before executing next time use method jtfmob.setText("")
, then write your complete logic.
Upvotes: 2