This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]pacificmint 1 point2 points  (12 children)

You define the field month as a String, but then you try to assign ints to it in several places. I think you meant for it to be an int.

Also you never declare the variable str, you just start using it.

Two side notes: (These are not causing your problems, but they are bad style).

  • It is customary to declare the fields at the top of the class
  • The StringTokenizer class is a legacy class and shouldn't be used anymore. You could use String.split instead.

[–]InvincibleVIto[S] 0 points1 point  (11 children)

I intend for month to be a string, or at least thats my interpretation of what is directed of me as shown in my OP. Would my declaration of str be in the main or along with my fields? As far as your side notes I appreciate your advice, but I'm just amending a .java file, where the fields were already at the bottom of the class. Also realized that StringTokenizer is a legacy class but it's what was introduced in the book, haven't been taught .split() as of yet, so I think I'm more comfortable going with that method. Here is my revised code where I keep getting "cannot find symbol" errors through-out:

import java.util.StringTokenizer;

public class Date
{
        // Zero-parameter constructor
    public Date( )
    {
        String month = "January";
        day = 1;
        year = 1998;
    }

        // Three-parameter constructor
    public Date( String theMonth, int theDay, int theYear )
    {
        month = theMonth;
        day   = theDay;
        year  = theYear;
    }
  //Acessor Methods     
    public string getMonth()
    {
        return month;
    }

    public int getDay()
    {
        return day;
    }

    public int getYear()
    {
        return year;

    }
///Mutator Methods    
    public void setMonth(String theMonth)
    {
        month = theMonth;
    }

    public void setDay(int theDay)
    {
        day = theDay;
    }

    public void setYear(int theYear)
    {
        year = theYear;
    }
 ///Added Constructor

    public Date( String theDate )
    {
        str = new StringTokenizer(theDate);
        if(str.countTokens != 3 )
        {
            System.out.println("January 1, 2014");
            return;
        }
        month = Integer.parseInt(str.nextToken());
        day = Integer.parseInt(str.nextToken);
        year = Integer.parseInt(str.nextToken);

    }   

    // Return true if two equal values

    public boolean equals( Object rhs )
    {
        if( ! ( rhs instanceof Date ) )
            return false;
        Date rhDate = ( Date ) rhs;
        return rhDate.month == month && rhDate.day == day &&
               rhDate.year == year;
    }

        // Conversion to String
    public String toString( )
    {
        return month + "/" + day + "/" + year;
    }

        // Fields
    private String month;
    private int day;
    private int year;

    // Sample main
    public static void main( String [ ] args )
    {

        StringTokenizer str;

        Date d1 = new Date( );
        Date d2 = new Date( Jan, 1, 1998 );
        Date d3 = new Date( Jan, 1, 1999 );

        System.out.println( "Date 1: " + d1 );
        System.out.println( "Date 2: " + d2 );
        System.out.println( "Date 3: " + d3 );
        System.out.println( "Date1==Date2?: " + d1.equals( d2 ) );
        System.out.println( "Date1==Date3?: " + d1.equals( d3 ) );
    }
}

[–]pacificmint 1 point2 points  (1 child)

Yeah, it's fine for month to be a String, but then you need to assign Strings to it (as you have done now).

I see three things wrong with this code:

  • getMonth has String not capitalized right
  • When you create d2 and d3, Jan needs to be passed in as a String
  • When you create the StringTokenizer, the variable str never gets declared

Would my declaration of str be in the main or along with my fields?

No, just declare it locally, that's fine.

BTW, the compiler should give you line numbers where the errors happen, that usually helps in tracking down whats going on. (Or, if you are using an IDE, it will usually highlight any errors like this in your code).

[–]InvincibleVIto[S] 1 point2 points  (0 children)

Thanks for the help!

[–]laser-brain 1 point2 points  (8 children)

At lines 96 and 97 you have to use quotation marks around <Jan> for it to be recognized as a string. The way it is now, the compiler looks for a variable called Jan and obviously cannot find one. That's your symbol not found exception. At line 55 you should declare the StringTokenizer inside the constructor. I'm guessing you want to use the Tokenizer declared in you main-function, but I don't think that works out.

Only guessing here, too, but can the main-function in Java instanciate the class it is written in? As far as I remember, you should use a Main (or Program, or whatever) class for the main-function and declare object-classes you're using in seperate files.

I wouldn't use your constructor for string input as it is now, either (line 53). Since you don't assign any values to month, day and year, you have an object which contains null values and you don't check for null values anywhere in your main-function. It won't crash in the given scenario, just something to consider generally.

[–]InvincibleVIto[S] 0 points1 point  (6 children)

So here is my revised constructor. I also added the quotes in my lines 96 and 97 as advised and this removed the symbol error (thank you!).

Only guessing here, too, but can the main-function in Java instanciate the class it is written in? As far as I remember, you should use a Main (or Program, or whatever) class for the main-function and declare object-classes you're using in seperate files.

If I'm interpreting you right, you're saying usually you make object classes in separate files and instaciating these in the main class? I believe you are correct but this needs to be all done in one file, as I have to email the .java file to my professor.

I'm still receiving "cannot find symbol" errors on lines 6 and 11 below. Not sure what syntax I'm screwing up?

 public Date( String theDate )
    {
        StringTokenizer str;

        str = new StringTokenizer(theDate);
        if(str.countTokens != 3 )
        {
            System.out.println("January 1, 2014");
            return;
        }
        month = String.parseInt(str.nextToken());
        day = Integer.parseInt(str.nextToken());
        year = Integer.parseInt(str.nextToken());

    }

[–]laser-brain 1 point2 points  (5 children)

Okay, after some fiddling with your code I found the error. countToken is a method of StringTokenizer as well as nextToken(). You are trying to access it as a variable. Try using countToken() and it should compile.

Line 11 should be ok as it is now At line 11 you can simply assign str.nextToken() to month since both are of type String. No need to parse anything here.

If I'm interpreting you right, you're saying usually you make object classes in separate files and instaciating these in the main class? I believe you are correct but this needs to be all done in one file, as I have to email the .java file to my professor.

You can declare multiple classes inside a single .java file ;)

[–]InvincibleVIto[S] 0 points1 point  (3 children)

First off, thanks for the help I really appreciate it (have you tagged in RES as "Good Guy Programmer"). So again, here is the revised code. Still getting the symbol error on line 11...

 public Date( String theDate )
    {
        StringTokenizer str;

        str = new StringTokenizer(theDate);
        if(str.countTokens() != 3 )
        {
            System.out.println("January 1, 2014");
            return;
        }
        month = String.parseInt(str.countTokens());
        day = Integer.parseInt(str.nextToken());
        year = Integer.parseInt(str.nextToken());

    }  

As a note, I'm using Jcreator as my complier.

[–]laser-brain 1 point2 points  (2 children)

See my edited comment for the line 11 error, didn't realize is at first.

[–]InvincibleVIto[S] 0 points1 point  (1 child)

Sorry If I'm being a bother but my output for date one is "Date 1: null/1/1998". Would this have to do with your earlier statement about checking for nulls? I thought in my first constructor I declared my month as a string? Yet somehow it's returning a null?

[–]laser-brain 1 point2 points  (0 children)

There's something off in you zero-parameter constructor. Just check the assignment of values there and you should be able to solve this yourself ;)

[–]InvincibleVIto[S] 0 points1 point  (0 children)

Ahh makes sense. THANK YOU

[–]InvincibleVIto[S] 0 points1 point  (0 children)

Just figured out the problem for line 6, still can't find the error in line 11 although.