Ok so here is the problem:

i am working on a code that will read a date from the keyboard in MM//DD//YYYY format. and then determines if it is a valid date or not.

So far I have gotten:

import java.util.Scanner;

/**
 *  Program:    UseCheckDate
 *  File Name:  UseCheckDate.java
 *  Author:     
 *  Date:       03/18/2009
 *  Description:Java program that takes infromation from a user and determines
 *              if the date is valid, and uses CheckDate class to further
 *              determine if the date is valid.
 */
public class UseCheckDate {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // Set up a way to get information from the user
        Scanner stdin = new Scanner(System.in);

        //Give directions to user on what to enter into the computer
        System.out.println("Enter a date in MM/DD/YYYY fromat.");

        // set a location to store infromation given by user
        String checkDate =  stdin.next("");

        //String name.trim();

        // an object to hold the amount of dates processed.
        int datesProcessed = 0;

        // setDate mutator method. Needs to be passed a single parameter,
        //the date string read from the keyboard. The setDate method is defined
        //in the CheckDate class.


        //void method named verify. Used to verify the validity of the date
        // string and either display a message saying the date is a valid or
        //will display one or more error messages indicating waht is wrong with
        //the date.






    }

}

That is the main method for my program. There is another one that I am working on that goes with this. But I am having a hard time to figure out where I go from here.

I just need a little reminder of where to go and I think I can get it. If you have any good ideas, partial ideas, or hints on where to look it would be appreciated. Thank you for the time. Please let me know if you need the other section of code to go with this in order to understand it better.

Thanks again.

Recommended Answers

All 11 Replies

Use the String class's toCharArray method, putting the entire String into a character array. Then you can check each index to make sure the correct thing is at the index. For the indexes that contain numbers, you can use the Character class's isDigit method. There is also a more elegant solution that involves Patterns, but I don't know a lot about them, so I'll leave you with that (and my solution, while maybe not the best, will suffice and isn't very hard to implement).

Java's SimpleDateFormat class does exactly that - do you need to implement it yourself?

Java's SimpleDateFormat class does exactly that - do you need to implement it yourself?

Yeah we have to do it without the class that Java already has. The SimpleDateFormat class has the hours, minutes and seconds of the date and we don't need that information.

I have looked around to try and find a section of code that I could look at to help me visualize where to go, and I just keep getting lost.

I admit that I am new to programming and somethings are easy for me and others are difficult that would be very easy for someone else.

Thank you for all your help.

nightgoddess

Use the String class's toCharArray method, putting the entire String into a character array. Then you can check each index to make sure the correct thing is at the index. For the indexes that contain numbers, you can use the Character class's isDigit method. There is also a more elegant solution that involves Patterns, but I don't know a lot about them, so I'll leave you with that (and my solution, while maybe not the best, will suffice and isn't very hard to implement).

I have never worked with the character array's. I understand the part about using the character's class isDigit method in theory. We went over a section of that a couple of weeks ago. I just have a hard time getting from theory to implementation.

Thank you for your help.

nightgoddess

Here is an update on this program...

It uses a main method and another method to make it work. Currently I am just worrying about the main method which is what I have already posted. once I have that going I will concentrate on the other method.

I am still very new to using Java and some of the terminology is still Greek to me. I appreciate all the help and patience from everyone here. Thank you for your time and suggestions.

nigutgoddess

If I can make a suggestion: beginners always sart with the main method and try to work down to the code that does the real work. This is often more complicated, because the final version of the main method needs all the lower-level code, and that hasn't been written, or even designed, yet.
You may find it easier, more logical, and ultimately a lot faster, to start with the critical method called verify. |You can write the verify method first, and test it by hard-coding a few cases, for example:

public static void main(String[] args) {
   verify("16//05//2009"); // should be OK
   verify("complete rubbish");
   verify("29//02//20092); // not a leap year
}
static boolean verify(String dateString) {
   .. you write this and get it working first
}

Also, rather than try to get all the display of error messages right first time, just put in a System.out.println("Display error xxx"); so you can see if the code works, then put in the full error messages afterwards.
Once you have a working method, you can then move up a level and write AND TEST the code that gets the input and calls verify.

The overall strategy is to break it all down into the smallest possible steps so you can design, write, test, and debug each one before moving on to the next.
Hope this helps
J

Thank you J. That is very helpful. I didn't realize I was setting myself up for a harder time by writing the main method first. But your suggestions help. I will let you know where I get from this.

N.G.

nightGoddess:

1. Prompt the user for the date, and read it into a String.
2. Break the String into a character array. Example:

char[] theDate = yourString.toCharArray();
// If your String was 8/14/88, this char array will contain the following: [8][/][1][4][/][8][8]

3. Check the character array to make sure it matches the format you expected. For example,

//This assumes you have a char array (from above) called theDate
boolean isItADigit = Character.isDigit(theDate[0]);
//theDate[0] looks at the first index of theDate, which happens to be "8" (from above), which means it is a digit, so that method returns true.

Hope that helps.

Ok so I have been trying a few things and I am still stuck. Here is what I have saved so far. I have tried to implement a few of the ideas given here, but my program builder keeps giving me error messages. Apparently I am not quite getting it right and I don't know how to fix it.

import java.util.Scanner;

/**
 *  Program:    UseCheckDate
 *  File Name:  UseCheckDate.java
 *  Author:     Kie Bergman
 *  Date:       03/18/2009
 *  Description:Java program that takes infromation from a user and determines
 *              if the date is valid, and uses CheckDate class to further
 *              determine if the date is valid.
 */
public class UseCheckDate {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // Set up a way to get information from the user
        Scanner stdin = new Scanner(System.in);

        //Give directions to user on what to enter into the computer
        System.out.println("Enter a date in MM/DD/YYYY fromat.");

        // set a location to store infromation given by user
        String checkDate =  stdin.next("");

        String name.trim();//not sure why I am supposed to use this, 
        //but a friend told me it needed to be in the code somewhere.

        // an object to hold the amount of dates processed.
        int datesProcessed = 0;

        // setDate mutator method. Needs to be passed a single parameter,
        //the date string read from the keyboard. The setDate method is defined
        //in the CheckDate class.


        //void method named verify. Used to verify the validity of the date
        // string and either display a message saying the date is a valid date
        //or will display one or more errors indicating what is wrong
        //with the date.


static

    protected String checkDate;

    /**
     * Get the value of checkDate
     *
     * @return the value of checkDate
     */
    public String getCheckDate() {
        return checkDate;
    }

    /**
     * Set the value of checkDate
     *
     * @param checkDate new value of checkDate
     */
    public void setCheckDate(String checkDate) {
        this.checkDate = checkDate;
    }

Any further input on this would be great. Keep in mind that I am not familiar with all the terms for I am still a noob and I get confused easy. lol

Thank you all again for the help.

N.G.

The Javadocs are your best friend. Here is the Javadoc for the Scanner class. You'll notice that the next() method does not take any arguments (what goes inbetween the parentheses). (This means that your code, where you have String checkDate = stdin.next(""), should say String checkDate = stdin.next(). Look at the Javadoc to see why - as you can see in the Javadoc, the Scanner class's next() method does not have any arguments.

A little further down, you have the line String name.trim();. This will result in a compiler error, because the syntax (wording) of that statement is not allowed. String is a type of Object. You can create new variables of that type of Object by saying String myString = new String("this is what the string will say!");. What you have done by saying String name.trim(); is told the compiler what type of Object you would like to create (String), but you did not give the String a variable name. Also, what your friend was trying to point you towards can be found here. You will notice, if you read the trim() method, that it removes whitespace from a String. So, in the earlier example, if someone entered " 8/14/1988 ", the String's trim method would return "8/14/1988".

Example of trimming white space from a String:

//Create a new string
String myString = new String("  Unwanted white space!!  ");
//Remove the white space from the String by using the String's trim method. The trim method returns the String with the space gone.
String withoutTheSpace = myString.trim();
//Set the original String to the one without the spaces.
myString = withoutTheSpace;

A third error is that you declared the String checkDate two separate times. Variables can only be declared one time in each class.


Also, at this point, you have more than enough information to complete the project. If you don't understand terminology, or what the examples are doing, feel free to ask. But if you do not understand the basic concept of what an Object is, or how to use an Object that you have created to call methods, you should read a beginner Java tutorial before continuing to ask more specific questions. (I'm not trying to be rude, I'm just not sure where you are at this point).

Thank you for the input. Very helpful. I will let you know when I have gotten further on the program. hopefully I will be able to write this all off as solved soon.

N.G.

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.