Итак, я пытаюсь ввести данные о дате рождения в формате DD/MM/YYYY, длина ввода не может превышать 10 символов, и я должен убедиться, что это в правильном формате, чтобы пользователь не вводил что-то вроде A51/1/1982, я думаю, что у меня это написано правильно, по крайней мере, до моего сведения :), но я навсегда застрял в цикле while при проверке вводаЗастрял во время цикла при попытке проверить ввод строки
System.out.println("Please enter your date of birth in the format DD/MM/YYYY");
dateOfBirth = scanner.nextLine(); //Read date of birth
dob0 = dateOfBirth.substring(0);// to check char 0 is between 0-3
dob1 = dateOfBirth.substring(1);// to check char 1 is between 0-9
dob2 = dateOfBirth.substring(2);// to check char 2 is between/
dob3 = dateOfBirth.substring(3);// to check char 3 is between 0-1
dob4 = dateOfBirth.substring(4);// to check char 4 is between 0-9
dob5 = dateOfBirth.substring(5);// to check char 5 is between/
dob6 = dateOfBirth.substring(6);// to check char 6 is between 1-2
dob7 = dateOfBirth.substring(7);// to check char 7 is between 0-9
dob8 = dateOfBirth.substring(8);// to check char 8 is between 0-9
dob9 = dateOfBirth.substring(9);// to check char 9 is between 0-9
dob = dateOfBirth.length(); //convert string to int to check length
while (dob !=10 || !dob0.matches("[0-3]+") || !dob1.matches("[0-9]+") || !dob2.matches("[/]+")
|| !dob3.matches("[0-1]+") || !dob4.matches("[0-9]+") || !dob5.matches("[/]+")
|| !dob6.matches("[1-2]+") || !dob7.matches("[0-9]+") || !dob8.matches("[0-9]+")
|| !dob9.matches("[0-9]+"))//check all values
{
System.out.println("Please make sure you enter your date of birth in the format DD/MM/YYYY");
dateOfBirth = scanner.nextLine();
}
Я предполагаю, что я, вероятно, ушли давно далеко вокруг этого БСП любая помощь будет оценена :)
Благодарности
Большое спасибо, я знал, что я не переусердствовать, пытаясь понять это :) Я пошел с заявлением while (! dateofbirth.matches просто потому, что мы еще не покрыли SimpleDateFormat в моем курсе :) Еще раз спасибо :) – Chris