Hello, I'm trying to write a method that resets a string to a default value (previously defined in a class) if the value inputted is not a valid.
Because I know the format or structure of a valid input, I should easily be able to do this. The string is a date with the following structure
##-##-##, and using the split() method in the String class, I can create an array with three elements provide i use the hyphen as my delimiter. All that remains is to check if each element has a length that is exactly 2.
Does that sound like it would work? Because it doesn't. I'm not sure what to do about this because me reasoning seems to be correct, and I can't find any error in my code. The method is below.
public void setDate(String b)
{
String[] a = b.split("-");
for(int i = 0; i < a.length; i++)
{
if(a[i].length() != 2) { this.date = "00-00-00";}
else { this.date = b;}
}
}