Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java用户输入YY-MM-DD格式_Java_Regex_String_Date_Comparison - Fatal编程技术网

Java用户输入YY-MM-DD格式

Java用户输入YY-MM-DD格式,java,regex,string,date,comparison,Java,Regex,String,Date,Comparison,这是关于数据结构的作业,我似乎找不到我需要的确切答案 我正在尝试获取用户输入的日期,最好是YY-MM-DD格式,然后检查有效性。。如果无效,则循环直到有效。这看起来真的很乏味,我收到了两个非法开始的表达式错误 我在Google和StackOverflow上搜索了一下,但只找到了一些关于日期的东西 public String hireDate(){ Scanner input = new Scanner(System.in); boolean answer = false;

这是关于数据结构的作业,我似乎找不到我需要的确切答案

我正在尝试获取用户输入的日期,最好是YY-MM-DD格式,然后检查有效性。。如果无效,则循环直到有效。这看起来真的很乏味,我收到了两个非法开始的表达式错误

我在Google和StackOverflow上搜索了一下,但只找到了一些关于日期的东西

public String hireDate(){
    Scanner input = new Scanner(System.in);
    boolean answer = false;
    while(answer == false){
        String temp = input.nextLine();
        if((temp.charAt(0) >= 0 && temp.charAt(0) <= 9) && (temp.charAt(1)
                >= 0 && temp.charAt(1) <= 9) && (temp.charAt(2) == -) 
                && (temp.charAt(3) >= 0 && temp.charAt(3) <= 9) && (temp.charAt(4)
                >= 0 && temp.charAt(4) <= 9) && (temp.charAt(5) == -)
                && (temp.charAt(6) >= 0 && temp.charAt(6) <= 9)){
            answer = true;
        } else {
            answer = false;
        }    
    }
    return temp;
}
公共字符串hireDate(){
扫描仪输入=新扫描仪(System.in);
布尔答案=假;
while(答案=false){
字符串temp=input.nextLine();

如果((temp.charAt(0)>=0&&temp.charAt(0)=0&&temp.charAt(1)=0&&temp.charAt(3)=0&&temp.charAt(4)=0&&temp.charAt(6)您可以用正则表达式轻松地完成。只需检查您的输入字符串是否匹配您的模式(YY-MM-DD)


你可以很容易地用正则表达式来实现。只需检查你的输入字符串是否与你的模式匹配(YY-MM-DD)

您正在将字符与整数进行比较


您正在将字符与整数进行比较。

在任何特定编程语言中?请添加适当的标记。在任何特定编程语言中?请添加适当的标记。
if (temp.matches("\\d{2}-\\d{2}-\\d{2}")) 
temp.charAt(1)>=0