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!

if (propertyID.length < 1)
propertyID is an array, so you are just testing its declared number of elements.
I guess you intended to test the length of the String propertyID[numberOfPropertiesInArray]

if (propertyIDTxt.length < 1) {
            JOptionPane.showMessageDialog(null, "Missing ID!", "Error!", JOptionPane.WARNING_MESSAGE);
        } else {
            propertyID[numberOfPropertiesInArray] = propertyIDTxt.getText();    
             }

Gives me an error underneath 'length' saying "cannot find symbol"

Where am I going wrong?

.length gives you the size of an array.
To get the size of a String you need to call a suitable method of the String class - remember method calls always have a ( and a ).

if (propertyIDTxt.equals("")) {
            JOptionPane.showMessageDialog(null, "Missing ID!", "Error!", JOptionPane.WARNING_MESSAGE);
        } else {
            propertyID[numberOfPropertiesInArray] = propertyIDTxt.getText();    
             }
            
            if (propertyAddressTxt.equals("")) {
            JOptionPane.showMessageDialog(null, "Missing Address!", "Error!", JOptionPane.WARNING_MESSAGE);
        } else {
           propertyAddress[numberOfPropertiesInArray] =  propertyAddressTxt.getText();
             }

Didn't work, am I on the right track now or still completely wrong?

I guess propertyIDTxt is a text input field of some kind? In which case you should be testing its text, not the field itself.

Yeah it's my JTextField, oops!

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[numberOfPropertiesInArray].equals("")) {
            JOptionPane.showMessageDialog(null, "Missing ID!", "Error!", JOptionPane.WARNING_MESSAGE);
        } else {
            propertyID[numberOfPropertiesInArray] = propertyIDTxt.getText();    
             }
            
            if (propertyID[numberOfPropertiesInArray].equals("")) {
            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);
       }
   }

Tried this, it understands that the text is missing and produces the appropriate error messages, but it still continues to writeToProperty(filename); how do I 'break' the method so it does not continue?

There are many ways, but why not go with the easiest to follow and most generic?
create a boolean dataIsValid = true
if you find bad data set it to false
test it before writing to the file.

But in this specific case you could return; from the method immediately after reporting an error.

That's brilliant I will try that and if successful mark the thread solved, thanks a lot James!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.