Take the 2-minute tour ×

Hello everyone I have an intriguing problem.. It is my second semester in object oriented programming. My first project in my intro to java course involves creating a Date class that results in calculating the days elapsed from January 1st of that year. I have to obviously check for leap years and validate incorrect input. I am currently trying to check if the user enters too few or too many elements (within one string). This is what i have but the logic is flawed somewhere. When i enter too few elements it displays error and read again.. Then I enter too much it displays error and reads again. Then when i enter three elements it displays the previous error displayed.. After one error it does not accept that only 3 elements have been entered. Help please.

/* Accepts a string as an argument and splits it into 3 sections
 * month,day, and year
void setDateFields(String dateString){ 
    String [] a  = {null};              // Array created to hold dateString
           a = dateString.split(" ");   // Split dateString into three sections
                                        // each ending with a white space

    // While to check if user entered month day and year
    while (a.length != 3){
             if(a.length < 3)
                  System.out.println("Insufficient number of elements\n" +
                                     "Enter a new date in the format of MM DD YYYY");
             else if(a.length > 3) 
                  System.out.println("Too many elements entered\n" +
            a = dateString.split(" ");
    monthText = a[0];                   // The monthText is assigned the first index of the array
    dayText = a[1];                     // The dayText is assigned the second index of the array
    yearText = a[2];                    // The yearText is assigned the third index of the array4

    numericMonth = Integer.valueOf(monthText);
    numericDay = Integer.valueOf(dayText);
    numericYear = Integer.valueOf(yearText);
share|improve this question

closed as too localized by Stephen C, nfechner, Lukas Knuth, Daniel Kelley, Soner Gönül Feb 11 '13 at 12:55


What does readDate() do? –  Jack Maney Feb 11 '13 at 2:40
Why bother separating declaration from definition here? String[] a = dateString.split(" "); reads much better. –  Yuushi Feb 11 '13 at 2:42
readDate(); reads a string from the user and only returns the string if its contents are purely integers –  Ever Martinez Feb 11 '13 at 4:21
@EverMartinez - Well, you aren't assigning the return value of readDate() to a variable, so that's one problem right there. –  Jack Maney Feb 11 '13 at 5:54

1 Answer 1

Your function setDateFields always uses the first string you passed in. You need to get another string from the user (I presume readDate() does that, but it can't modify anything inside setDateFields()).

Your main line code should look something like this:

 do {
   dateString = readDate();
} while(!checkDateString(dateString));

checkDateString() should just check the passed in string and return true or false.

share|improve this answer
Yes I do have it to defined when i declare i must have posted a previous version.. And i do see that my readDate(); method does not modify anything.. I will modify and see if that helps –  Ever Martinez Feb 11 '13 at 4:17
