I am just learning Java and am trying to use variables from objects that I passed into another method. Can someone help me here

package data;


public class Data {

    private int day, month, year;
    
    public Data(int day, int month, int year)
    {
        this.day = day;
        this.month = month;
        this.year = year;
    }
    
    public int getDay()
    {
        return day;
    }
    public int getMonth()
    {
        return month;
    }
    public int getYear()
    {
        return year;
    }
    void setDay(int day)
    {
        this.day = day;
    }
    void setMonth(int month)
    {
        this.month = month;
    }
    void setYear(int year)
    {
        this.year = year;
    }
}
    
    int compareTo (Date current, Date former)
{
    if(current.year < former.year)
    {
        return 0;
    }
    else
        if(current.month < former.month)
        {
            return 0;
        }
    else
            if(current.day < former.day)
            {
                return 0;
            }
    else
                if(current.year == former.year && current.month == former.month 
                        && current.day == former.day)
                {
                    return -1;
                }
    else
                {
                    return 1;
                }
    
                }

It gives an error on the line with the close bracket after the return statements and on the line 41. Everything else works fine.

Dani AI

Generated

A couple of things to note beyond the original replies: the compiler errors were caused by two separate issues that often show up together — a stray closing brace that ended the class early (so the compare routine was written outside any class) and a name mismatch between the class (Data) and the method parameter type (Date). When a method is outside the class it can't access that class's private members, which explains the "cannot find symbol" messages. and caught the brace problem and the name mismatch that later corrected.

A clearer, idiomatic approach is to put the comparison logic inside the Data class and implement Comparable<Data>. Using Integer.compare keeps the code concise and follows the compareTo contract (negative = earlier, zero = equal, positive = later):

public class Data implements Comparable<Data> {
    private int day, month, year;
    // constructor + getters omitted

    @Override
    public int compareTo(Data other) {
        int cmp = Integer.compare(this.getYear(), other.getYear());
        if (cmp != 0) return cmp;
        cmp = Integer.compare(this.getMonth(), other.getMonth());
        if (cmp != 0) return cmp;
        return Integer.compare(this.getDay(), other.getDay());
    }
}

If a two-argument comparison is preferred (for use outside the class), make it a static helper or a Comparator<Data> and call the public getters so private fields are not accessed from another class:

public static int compare(Data a, Data b) {
    int cmp = Integer.compare(a.getYear(), b.getYear());
    if (cmp != 0) return cmp;
    cmp = Integer.compare(a.getMonth(), b.getMonth());
    if (cmp != 0) return cmp;
    return Integer.compare(a.getDay(), b.getDay());
}

Quick debugging checklist: verify matching braces (use an editor with brace matching), ensure class name and file name/signatures match, keep methods inside their classes, prefer getters when accessing private fields from other classes, and follow the Comparable contract or use a Comparator for external comparisons.

Recommended Answers

All 5 Replies

} on line 39 matches the { on line 4 and closes the definition of the class. Then there are more methods - but you can't have a method outside a class.

What kind of error?

EDIT: Oops, hadn't seen JamesCherrill's post

@ JamesCherrill. Thanks. It was in the class before, but when inside the class, all the object variables eg. current.month give an error:
cannot find symbol
symbol: variable month
location: variable current of type data.Date.

"Date" != "Data" - check your method signatures vs class name

Wow. I can't believe I didn't notice that. It is working fine now. Thanks

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.