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!

This is what I have so far, but I am getting stuck

import java.util.StringTokenizer;
import java.util.Scanner;

public class CheckDate
{
 public int validMonth;
 public int validDay;
 public int validYear;

 Scanner input = new Scanner(System.in);

 public CheckDate(input.nextLine())
 {

You 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));

Ok, now what if I want to tokenize them into three parts so it becomes mm/dd/yyyy? I've looked online for some examples and this is what I saw, but I don't know if she wants us to use the split method. She hasn't gone over this with us yet and our assignment i due before next class, so I am extremely lost and the book doesn't go into to much detail. This is what I have so far:

/**Lab 08
 * Class CheckDate that stores a user-input date as a String in the format mmddyyyy
 */

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

public class CheckDate
{
 public int validMonth;
 public int validDay;
 public int validYear;



 public CheckDate(String date)throws InvalidDateException 
 {

 StringTokenizer dateToken = new StringTokenizer(date, "/");

         int validMonth = Integer.parseInt (dateToken.nextToken().trim());
         int validDay = Integer.parseInt (dateToken.nextToken().trim());
         int validYear = Integer.parseInt (dateToken.nextToken().trim());



       if ((validMonth == 4 || validMonth == 6 || validMonth == 9 || validMonth == 11) &&(validDay > 30))
          {
             throw new InvalidDateException("Day value must be greater than 0 and less than 30");
           }
              else
               {
                      setValidDate(validDay, validMonth, validYear);
                }

        if (validMonth == 2 && validDay > 28)
           {
            throw new InvalidDateException ("Day value must be greater than 0 and less than 28");
        }
         else if(validMonth > 31)
         {
           throw new InvalidDateException ("Day value must be greater than 0 and less than 31");
         }
         else
         {

            setValidDate(validDay, validMonth, validYear);
         }

       if(validMonth < 1 || validMonth > 12)
         {
            throw new InvalidDateException("Month value must be greater than 0 and less than 12");
          }
             else
             {
                  setValidDate(validDay, validMonth, validYear);
             }


       if(validYear < 2010)
       {
           throw new InvalidDateException("Year must be greater than 2010");
        }
            else
           {
               setValidDate(validDay, validMonth, validYear);
           }
 }//end constructor

  public void setValidDate(int day, int month, int year) 
    {

     validDay = day;
     validMonth = month;
     validYear = year; 
    }

    public String getValidDate()
    {
      return (validMonth + "/" + validDay + "/" + validYear + "/");
    }

    public String getLongDate()
    {

    }

 }

She also wants us to use Stringbuilder to conver mm/dd/yyyy to a long date format.

You 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.

The user is asked to input a date with "/", so that is why I used it that way.

OK,I was confused when you said " a user-input date as a String in the format mmddyyyy"
If the user input is mm/dd/yyyy then you can just use String's split method to separate the three parts.

When I tried to use the split method it would only grab the 0 from lets say 06/12/2011, and then throws an excption, instead of grabbing the 6.

Obviously an error in your (undisclosed) code. There's nothing wrong with split.

"06/12/2011".split("/") splits correctly - try executing

System.out.println(Arrays.toString("06/12/2011".split("/")));

Well the test harness didn't have a print out to the arrays though. One of the reasons I am confused by the split methods.

I just showed you the print so you could see how the split method returns its results (and see that it is working), that's all.

Ok, I see now ha! Thank you.

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.