I need to write a class that stores a user-input date as a String in the format mmddyyyy. The constructor validates the input, but it accepts a String input and it tokenizes it into three parts, all int variables and in order to do that I have to tokenize the String input. I am having trouble setting the constructor part of it. I get constructors, but I have never used one where there was input passed through it, so I'm confused on how to go about it. any help would be greatly appreciated!
brittney_2
0
Junior Poster in Training
Recommended Answers
Jump to PostYou declare the parameter(s) of a constructor just like an ordinary method eg
public CheckDate(String mmddyyyyString) { ...
then call them passing parameters also like an ordinary method, eg
CheckDate cd = new CheckDate(input.nextLine));
Jump to PostYou can't tokenise an mmddyyyy string by using "/" as a delimiter, becuase the string doesn't contain any "/" characters!
Because the format is fixed, you can simply use String's substring method to pick out each of the three parts, then parse each of those into int values.
All 11 Replies
brittney_2
0
Junior Poster in Training
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
brittney_2
0
Junior Poster in Training
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
brittney_2
0
Junior Poster in Training
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
brittney_2
0
Junior Poster in Training
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
brittney_2
0
Junior Poster in Training
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
brittney_2
0
Junior Poster in Training
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.