0

I have a homework assignment where we have to make a very basic console app for a fitness center, that can create members and employees. My problem occurs when i ask the user to type in the birthday of the new members. It is supposed to check if the birthday is in the format DD/MM/YY, so i check if the answer given is 6 digits, and 6 digits only. And it seems to work, unless someone gives a birth date that start with 0, EG.: 090498.

System.out.println("Please enter the new members birthday, in the format of DD/MM/YY:");
    this.birthdate = console1.nextInt();
    while(String.valueOf(birthdate).length() != 6){
    System.out.println("Please give a valid date of birth in the format of: DD/MM/YY");
    this.birthdate = console1.nextInt();
}   

It doesn't give a error message, but only completes the loop if the birthdate IS 6 digits, AND starts with a 1 EG.: 190798.

i know this is probably very simple, but i only started studying CS about a month ago :)

Nexevis
  • 4,647
  • 3
  • 13
  • 22
  • Yes, because you are accepting an int, and the initial 0 is dropped automatically. You should accept a string. – Andrea Oct 22 '19 at 13:09
  • Date formats like `DD/MM/YY` are not a single number but a combination of 3 numbers so if you want to have the date entered as one you need to use a string. This could then be checked with a regex to only allow valid numbers for each part. In contrast your method would allow me to enter a date of 999999 and this would be considered valid because it is a 6-digit number. – Thomas Oct 22 '19 at 13:13
  • A regex to check for plausible date input could be `^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[0-2])[0-9]{2}$`. This would alloww days in the range 01-31 and months in the range 01-12 but it would allow "invalid" dates like Feb 30th or April 31st so you'd either need a more complex one or - which I'd say would be way better - properly parse the input into a `Date` or at least into 3 integers which you then could feed to more thorough checks. – Thomas Oct 22 '19 at 13:21

3 Answers3

2

As the other answers told you if you start an int with 0 the code will ignore it. You can import a SimpleDate and with a String you can use it. I leave here a link on another question that maybe can help you more. [link] How can I convert an Integer (e.g 19000101 ) to java.util.Date?

import java.text.SimpleDateFormat;  
import java.util.Date; 

public class StringToDateEXE {  
public static void main(String[] args)throws Exception {  
    String DateT="31/12/1998";  
    Date NDate=new SimpleDateFormat("dd/MM/yyyy").parse(DateT);  
    System.out.println(DateT+"\t"+NDate);  
}  
}  

Hope it helped.

ICodeForCaffeine
  • 195
  • 2
  • 10
0

The problem is that you are accepting an int, and the initial 0 is automatically dropped. And a birthday should not be represented as an integer. My advice is to change that field as a String, and accepting a value of that type as a consequence.

Andrea
  • 6,032
  • 2
  • 28
  • 55
0

You are taking an int and that makes it drop the leading 0. You should accept a String instead. Here's the code corrected to do that

System.out.println("Please enter the new members birthday, in the format of DD/MM/YY:");
this.birthdate = console1.nextLine();
while(birthdate.length() != 6){
    System.out.println("Please give a valid date of birth in the format of: DD/MM/YY");
    this.birthdate = console1.nextLine();
}   

This assumes that you changed the type of birthdate to String. You should know that your validation strategy is a naive one since anyone can enter a length 6 string without it being a valid birthdate or a birthdate at all. Use regular expressions as in here for that kind of validations. Regular expressions can be applied to any string with the String.matches() method by passing to it the regular expression. A different approach would be to convert the integer into a date time object.

Javier Silva Ortíz
  • 2,864
  • 1
  • 12
  • 21