Hello,
I am trying to validate that the user has entered data into the JTextField to be saved to a text file.
My method is:
Data saved to Text File
Data loaded into an array upon execution
Data saved to array, then written to Text File.
My addProperty Method:
public void addProperty(){
try{
numberOfPropertiesInArray += 1;//increases the number of indexes in the array variable by + 1
//retrieves the information from each element[index specified by variable] then set it to what ever is in the text field.
propertyID[numberOfPropertiesInArray] = propertyIDTxt.getText();
propertyAddress[numberOfPropertiesInArray] = propertyAddressTxt.getText();
propertyAddress2[numberOfPropertiesInArray] = propertyAddress2Txt.getText();
propertyTown[numberOfPropertiesInArray] = propertyTownTxt.getText();
propertyCounty[numberOfPropertiesInArray] = propertyTownTxt.getText();
propertyPostCode[numberOfPropertiesInArray] = propertyCountyTxt.getText();
propertyNumBedrooms[numberOfPropertiesInArray] = propertyPostCodeTxt.getText();
propertyNumBathrooms[numberOfPropertiesInArray] = propertyNumBedroomsTxt.getText();
propertyNumFloors[numberOfPropertiesInArray] = propertyNumBathroomsTxt.getText();
propertyRent[numberOfPropertiesInArray] = propertyNumFloorsTxt.getText();;
propertyFeatures[numberOfPropertiesInArray] = propertyRentTxt.getText();
if (propertyID.length < 1) {
JOptionPane.showMessageDialog(null, "Missing ID!", "Error!", JOptionPane.WARNING_MESSAGE);
} else {
propertyID[numberOfPropertiesInArray] = propertyIDTxt.getText();
}
if (propertyAddress.length < 1) {
JOptionPane.showMessageDialog(null, "Missing Address!", "Error!", JOptionPane.WARNING_MESSAGE);
} else {
propertyAddress[numberOfPropertiesInArray] = propertyAddressTxt.getText();
}
writeToProperty(filename);//writes the informatio to file
}catch (Exception e) {//error message to catch errors
JOptionPane.showMessageDialog(null, "File Input Error", "Error!", JOptionPane.WARNING_MESSAGE);
}
}
This is the code I have so far, but when I execute the method it continues as normal even if I leave the two JTextFields blank.
Thank you for your help!