-3

Trying to format the numbers expression of [0-9]{2}[-]{1}[0-9]{2}[-]{1}[1-9]{4} so that the users input have to be of the format XX-XX-XXXX. Is the following expression above the correct way to do just so?

  try {

   int datum = Integer.parseInt(s);
        if (!strPersnr.matches("[0-9]{2}[-]{1}[0-9]{2}[-]{1}[1-9]{4}")) {

        throw new IllegalArgumentException("You printed wrong format, try again");
                    }
                    System.out.println("Processsing...");

                } catch (IllegalArgumentException e) {
                    System.out.println(e.getMessage());
                    valid = false;
                }

 } while (!valid);
  • Shouldn't there be some regex online matcher out there to check with? Edit: I googled for it and found [this](https://www.regextester.com/). The expression seems legit. Please put some time into answering stuff yourself before posting on SO. – akuzminykh Mar 16 '20 at 21:55
  • `strPersnr` never changes in this loop. It will either execute once, or forever. – David Conrad Mar 16 '20 at 21:59
  • Have you tried this code and expression? – NomadMaker Mar 16 '20 at 22:14
  • YES I HAVE , and what happens is that my console infinitely begins to print "you printed wrong format", so that number expression is wrong – Mind of Virtuoso Mar 16 '20 at 22:24
  • use `\d{2}-\d{2}-[1-9]{4}` – banan3'14 Mar 16 '20 at 22:48
  • Does this answer your question? [How to validate phone numbers using regex](https://stackoverflow.com/questions/123559/how-to-validate-phone-numbers-using-regex) – banan3'14 Mar 16 '20 at 22:49

1 Answers1

0

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();
    }

}
DevilsHnd - 退職した
  • 8,739
  • 2
  • 19
  • 22