2

I first want to validate that the user entered a value and to make sure to exit if 'cancel' was pushed. Then, I want to validate that the String releaseDateString is in the correct format at the same time as converting the String to java.sql.Date.

The first validation is taking place but then the JOptionPane carries on repeating itself and does not even consider the try and catch following it.

Here is my method

boolean retry = false;
java.sql.Date releaseDate = null;
String releaseDateString = "";
String title = "";

while (!retry) {
   while(!retry){//field is validated to make sure a value was entered and to exit if cancel was pushed
      releaseDateString = JOptionPane.showInputDialog("Please input the release date of the movie (yyyy-mm-dd)");
      qtd.stringValidation(releaseDateString);  
   }
   try { //the date is validated to make sure it is in the correct format
      releaseDate = java.sql.Date.valueOf(releaseDateString);
   } catch (Exception e) {
      retry = false;
      JOptionPane.showMessageDialog(null, "Make sure you enter a date in the format of 'dd-mm-yyy'");
   }
}

It links to this method

public static boolean stringValidation(String attribute){
   boolean retry = false;
   if (attribute == null){
      System.exit(0);
   }
   else if (attribute.equals("")) //if the cancel button is selected or no value was entered into the 
   {
      JOptionPane.showMessageDialog(null, "Make sure you enter a character into the textbox");
   }
   else {
      retry = true;
   }
   return retry;          
}
3
  • This has nothing to do with the problem, but stringValidation is a static method. You should probably not access it through qtd if qtd is an instance. You should access it through your class, like: MyClass.stringValidation. Commented Aug 11, 2014 at 19:22
  • qtd is actually the name of another class in the same package but thanks anyhow. Commented Aug 11, 2014 at 19:31
  • Than that's a pretty poor naming convention. Class names in Java follow the Pascal Case naming convention and should start with a upper case letter. Your current pattern will confuse everyone. Commented Aug 11, 2014 at 19:39

1 Answer 1

2

When you do this,

qtd.stringValidation(releaseDateString); 

You aren't assigning the result to retry. I believe you wanted,

retry = qtd.stringValidation(releaseDateString);
Sign up to request clarification or add additional context in comments.

1 Comment

Good answer. 1+ to counter my recent down-vote. Again, sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.