You seem to keep deleting (closing) your posts before an answer can be provided.
try this regex:
if (!strPersnr.matches("^(0?[1-9]|1[012])\\-(0?[1-9]|[12][0-9]|3[01])\\-[0-9]{4}$")) {
System.out.println("Invalid date format supplied! (" + strPersnr +
"). Format required: MM-DD-YYYY");
}
It ensures the date is entered in the format of MM-dd-yyyy
and the values supplied are valid.
If you want the format of dd-MM-yyyy
then use this one:
if (!strPersnr.matches("^(0?[1-9]|[12][0-9]|3[01])\\/(0?[1-9]|1[012])\\/[0-9]{4}$")) {
System.out.println("Invalid date format supplied! (" + strPersnr +
"). Format required: DD-MM-YYYY");
}
Note:
If you want to loop so that the User has the opportunity to supply a proper date then don't throw an exception. Instead, inform the User that the date supplied is an invalid format, show and example of a proper format, and allow that User to try again.
Overall I believe what you have been striving for all day is a class that looks something like this:
import java.util.Scanner;
public class PersonTidbok {
private final Scanner console = new Scanner(System.in);
public static void main(String[] args) {
// Done this way so everything doesn't
// need to be static.
new PersonTidbok().startApp(args);
}
private void startApp(String[] args) {
int persnrValue = 0;
boolean exit = false;
while (!exit) {
/* supposed to make sure that the main menu is brought back
after final method line has been executed. requests the
user to again input into console. */
String menuSeletion = mainMenu();
switch (menuSeletion) {
case "P":
String persnr = "";
while (persnr.equals("")) {
System.out.print("Enter persnr in the format of (YYYY-MM-DD): --> ");
persnr = console.nextLine();
if (!persnr.matches("^([0-9]{4})\\-(0?[1-9]|1[012])\\-(0?[1-9]|[12][0-9]|3[01])$")) {
System.out.println("Invalid date format supplied! (" + persnr
+ "). Format required: YYYY-MM-DD. Try Again...");
persnr = "";
}
}
persnrValue = Integer.parseInt(persnr.replace("-",""));
System.out.println("Processsing...");
// ...Processing of 'persnrValue' should go here
break;
case "T":
System.out.println("FINISH YOUR CODE");
break;
case "L":
System.out.println("FINISH YOUR CODE");
break;
case "S":
System.out.println("FINISH YOUR CODE");
break;
case "A":
System.out.println("FINISH YOUR CODE");
break;
case "Q": // Quit??
System.out.println("Bye-Bye");
System.exit(0);
}
}
}
// MAIN MENU
// Returns the selected menu item in Upper Letter Case
private String mainMenu() {
String input = "";
while (input.equals("")) {
System.out.print("Enter a letter: P, T, L, S, A, or Q: --> "); //menu
input = console.nextLine().trim();
/* Does the User input comprise of a single letter in
either upper or lower letter case which is P, T, L,
S, A, or Q. */
if (!input.matches("(?i)[PTLSAQ]")) {
// NO, it doesn't - Re-prompt...
System.out.print("You did not enter the correct menu option!");
input = "";
}
}
return input.toUpperCase();
}
}