-3

尝试格式化数字表达式,[0-9]{2}[-]{1}[0-9]{2}[-]{1}[1-9]{4}以便用户输入的格式必须为 XX-XX-XXXX。上面的以下表达式是正确的方法吗?

  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);
4

1 回答 1

0

在提供答案之前,您似乎一直在删除(关闭)您的帖子。

试试这个正则表达式:

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

它确保以 的格式输入日期MM-dd-yyyy并且提供的值是有效的。

如果你想要的格式dd-MM-yyyy然后使用这个:

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

笔记:

如果您想循环以便用户有机会提供正确的日期,那么不要抛出异常。相反,通知用户提供的日期格式无效,显示和示例正确格式,并允许该用户重试。

总的来说,我相信你一整天都在努力的是一个看起来像这样的课程:

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

}
于 2020-03-16T22:38:27.167 回答