I am programing a JavaFX application where I am connecting my JavaFX TableView to my Sqlite database, and here I am trying to do is that if the textfield is empty, then it asks the user to please fill all the data. However, because in my Sqlite database I have 2 sets of data set in integer and one as a double, I cannot invoke the isEmpty to for those three sets of data. However, I cannot cast them to a String, as I will need to do calculations later on. One other aspect which is the last bit of code, is when the user wants to fill in the data to add a new set of data, he can clear whatever he was writing in those text fields, however, I am not sure what is the code for integers and doubles instead of "expeditionDate.setText(null);". Thank you in advance.
String query = null;
Connection connection = null;
ResultSet resultSet = null;
PreparedStatement preparedStatement;
Expedition user = null;
private boolean update;
int studentId;
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@FXML
private void save(MouseEvent event) {
connection = DbConnect.connect();
String Date = expeditionDate.getText();
double Time = Double.valueOf(expeditionTime.getText());
int Altitude = Integer.parseInt(expeditionAltitude.getText());
int Distance = Integer.parseInt(expeditionDistance.getText());
String Notes = expeditionNotes.getText();
if (Date.isEmpty() || Time.isEmpty() || Altitude.isEmpty() || Distance.isEmpty()|| Notes.isEmpty()) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setHeaderText(null);
alert.setContentText("Please Fill All DATA");
alert.showAndWait();
} else {
getQuery();
insert();
clean();
}
}
@FXML
private void clean() {
expeditionDate.setText(null);
expeditionTime.setText(null);
expeditionAltitude.setValue(null);
expeditionDistance.setValue(null);
expeditionNotes.setText(null);
}
CodePudding user response:
Double.valueOf will throw exception if getText() is empty but to perform other checks & better null handling -
Change
double Time = Double.valueOf(expeditionTime.getText());
to
Double Time = Double.valueOf(expeditionTime.getText());
Same for int - change int Altitude to Integer Altitude
CodePudding user response:
IMHO you should not try to parse a literal into a numeric value if you are not sure if it is set correctly. Your problem is that you can't check if the value is empty, because primitive types don't have a built-in isEmpty
method.
private boolean isEmpty(String value) {
return value == null || value.trim().length() == 0;
}
I would use the above method to check if expeditionTime.getText()
is null or empty, and if so, use the default value ie. 0.0
or any default value you would like to have.
CodePudding user response:
Test to make sure the required data is in place before you even attempt to work the data and make a save or change to the Database:
@FXML
private void save(MouseEvent event) {
if (aFormFieldIsEmpty()) {
new Alert(Alert.AlertType.WARNING, "One or more fields on the form is empty!\n"
"Data must be provided in all fields (except Notes)!").showAndWait();
return;
}
connection = DbConnect.connect();
String Date = expeditionDate.getText();
double Time = Double.valueOf(expeditionTime.getText());
int Altitude = Integer.parseInt(expeditionAltitude.getText());
int Distance = Integer.parseInt(expeditionDistance.getText());
String Notes = expeditionNotes.getText();
getQuery();
insert();
clean();
}
@FXML
public boolean aFormFieldIsEmpty() {
return expeditionDate.getText().isEmpty() ||
expeditionTime.getText().isEmpty() ||
expeditionAltitude.getText().isEmpty() ||
expeditionDistance.getText().isEmpty();
}
@FXML
private void clean() {
expeditionDate.clear();
expeditionTime.clear();
expeditionAltitude.clear();
expeditionDistance.clear();
expeditionNotes.clear();
}