用户输入验证:日期输入

huangapple go评论106阅读模式
英文:

user input validation: date input

问题

以下是你的代码部分,我已经将其翻译好:

  1. private static void extracted2(Doctor l1, Patient p1, List<Schedule> lista) {
  2. Scanner sc2 = new Scanner(System.in);
  3. System.out.println("请提供日期,格式为 M/d/yyyy");
  4. while (true) {
  5. try {
  6. String userinput = sc2.next();
  7. DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("M/d/yyyy");
  8. LocalDate date = LocalDate.parse(userinput, dateFormat);
  9. Schedule g5 = new Schedule(5, l1, p1, date, false, false);
  10. lista.add(g5);
  11. for (Schedule x : lista) {
  12. System.out.println(x);
  13. }
  14. } catch (DateTimeParseException e) {
  15. System.out.println("输入有误,请重新输入正确的日期数据:" + e.getMessage());
  16. }
  17. }
  18. }

注意:我将DataFormatException更改为DateTimeParseException,因为在Java中,日期时间解析错误通常会抛出DateTimeParseException异常。

英文:

I have a method below which asks user to provide date from they keyboard, I need to extend my code to validate if user indeed entered the data in format M/d/yyyy.
If not, ask again to repeat and correct input data, how can I implement that?

  1. private static void extracted2(Doctor l1, Patient p1, List&lt;Schedule&gt; lista) {
  2. Scanner sc2 = new Scanner(System.in);
  3. System.out.println(&quot;provide data with format M/d/yyyy&quot;);
  4. while (true) {
  5. try {
  6. String userinput = sc2.next();
  7. DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(&quot;M/d/yyyy&quot;);
  8. LocalDate date = LocalDate.parse(userinput, dateFormat);
  9. Schedule g5 = new Schedule(5, l1, p1, date, false, false);
  10. lista.add(g5);
  11. for (Schedule x : lista) {
  12. System.out.println(x);
  13. }
  14. } catch (DataFormatException e) {
  15. System.out.println(&quot;Wrong data &quot; + e.getMessage());
  16. }
  17. }
  18. }

答案1

得分: 0

我认为你已经接近了,但这是最终的解决方案:

从代码中删除了其他逻辑,因为可以随后添加。

  1. Scanner sc2 = new Scanner(System.in);
  2. LocalDate date = null;
  3. boolean isValid;
  4. do {
  5. try {
  6. System.out.println("提供日期格式 M/d/yyyy");
  7. DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("M/d/yyyy");
  8. String userinput = sc2.next();
  9. date = LocalDate.parse(userinput, dateFormat);
  10. isValid = true;
  11. } catch (DateTimeParseException exception) {
  12. isValid = false;
  13. }
  14. } while(!isValid);
  15. System.out.println(date);

输出:

  1. > 提供日期格式 M/d/yyyy
  2. > 333
  3. > 提供日期格式 M/d/yyyy
  4. > 444
  5. > 提供日期格式 M/d/yyyy
  6. > 1/2/2020
  7. > 2020-01-02
英文:

I think you are close but here is the final solution:
Removed other logic from the code since it can be added.

  1. Scanner sc2 = new Scanner(System.in);
  2. LocalDate date = null;
  3. boolean isValid;
  4. do {
  5. try {
  6. System.out.println(&quot;Provide date format M/d/yyyy&quot;);
  7. DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern(&quot;M/d/yyyy&quot;);
  8. String userinput = sc2.next();
  9. date = LocalDate.parse(userinput, dateFormat);
  10. isValid = true;
  11. } catch (DateTimeParseException exception) {
  12. isValid = false;
  13. }
  14. } while(!isValid);
  15. System.out.println(date);

output:

> Provide date format M/d/yyyy
> 333
> Provide date format M/d/yyyy
> 444
> Provide date format M/d/yyyy
> 1/2/2020
> 2020-01-02

huangapple
  • 本文由 发表于 2020年4月5日 15:52:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/61039592.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定