简体   繁体   中英

Checking digits in string, doesnt work when the string starts with a 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:)

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.

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.

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 (eg 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.

 String birthdate = in.nextLine();
    while(String.valueOf(birthdate).length() != 6){
    System.out.println("Please give a valid date of birth in the format of: DD/MM/YY");
    birthdate = in.nextLine();
}   

or regex ^[0-3]?[0-9]/[0-3]?[0-9]/(?:[0-9]{2})?[0-9]{2}$

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM