我正在写一些代码来从键盘上读取日期。我以字符串形式读取它,然后使用我允许的所有分隔符的字符串将其拆分为标记,但是当用户点击输入按钮时我也想转到下一个标记。有没有办法将它用作分隔符?
这是我的方法:
public static String getEnrollDate() {
Scanner stdin = new Scanner(System.in);
int month;
String strM;
int day;
String strD;
int year;
String strY;
String enrollDate;
String delims = "[/ \n\r]+";
String[] tokens;
int count = 0;
do {
System.out.print("Please enter the enrollment date as MM/DD/YYYY, else enter 'none': ");
enrollDate = stdin.nextLine();
if (!(enrollDate.equalsIgnoreCase("none"))) {
count = 0;
tokens = enrollDate.split(delims);
month = Integer.parseInt(tokens[0]);
strM = tokens[0];
day = Integer.parseInt(tokens[1]);
strD = tokens[1];
year = Integer.parseInt(tokens[2]);
strY = tokens[2];
if (strM.length() > 2 || strD.length() > 2 || strY.length() != 4) {
System.out.println("Sorry, please enter the date in the specified format.");
count = 1;
}
}
} while (count == 1);
return enrollDate;
}