java if else errors not working properly

Welcome to StackOverflow,

At this point I’m assuming you have been rigorously reading all the suggested material pointed out by the fine people that have supplied you with comments to assist you in acquiring the proper help you seek. After all, you haven’t provided a response to some of the valid questions related to your post and, I highly suspect that I wont receive one after this post either.

In any case I’m going to provide you with some suggestions that “I think” would improve your your code somewhat and hey….maybe even getting it working properly. Without certain specifics we can only assume what the current dilemma with your provided code might be and therefore I’m just going to take a poke at it so to speak.

To begin with, I would never put all that code into an event method. I would place it into yet another method and perhaps call it something like: checkRegistration() and then call that method from the event. This is the more desirable way to do things in programming:

Here is your registerActionPerformed() method:

private void registerActionPerformed(java.awt.event.ActionEvent evt) {
    if (!checkRegistration()) {
       //Validation Failed. Do something here to
       //ensure proper data entry is carried out.

Easy on the eyes and allows for other items to be added to the event without washing through all the code clutter.

Now for the checkRegistration() method where your validation code is housed and called from the registerActionPerformed() method:

private boolean checkRegistration() {
    String eMsg = "";
    if (compName == null){ eMsg = "Company Name Required"; }
    else if(regNo == null){ eMsg = "Registration Number Required"; }
    else if(addressL1 == null){ eMsg = "Address Required"; }
    else if(postcode == null){ eMsg = "Postal Code Required"; }
    else if(username == null){ eMsg = "Username Required"; }
    else if(password == null){ eMsg = "Password Required"; }
    else {
        eMsg = saveRegistration();
    if (!"".equals(eMsg)) {
        JOptionPane.showMessageDialog(new JFrame(), eMsg, "Error",
        return false;
    return true;

Now, in the above method I’m assuming that you actually have variables declared somewhere which already contain the pertinent values such as compName, addressL1, postcode, etc. If you are pulling the data directly from specific GUI components then you will need to ensure that the variables being validated within all the ‘if‘ and ‘else if‘ statements are properly declared and filled before calling this method or, established within the method itself before the ‘if‘ and ‘else if‘ statements are processed.

For the actual saving of the registration data I have placed this code in yet another method conveniently named saveRegistration() and it is called from the final ‘else’ statement should all the validating succeed. It’s better to do it this way because the method to save registration can then be called from anywhere within the class:

private String saveRegistration() {
    String eMsg = "";
    try {
        File user = new File(username.getText()+".txt");
        BufferedWriter output = new BufferedWriter(new FileWriter(user));
        //........... your write code ............
    catch (IOException IOe) { 
        eMsg = "Error Writing To File!\n\n" + IOe.getMessage(); 
    return eMsg;

Well Mihai, I hope this has helped you somewhat.

Browse More Popular Posts

Leave a Comment