Results 1 to 7 of 7
  1. #1
    erin.ctm is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Unhappy cant get compareTo to read false

    Hi there! During my test, no matter what date I enter, the 1st if statement reads true so System.out.println(myBdate + " and " + birthDate2 + " are the same."); prints. myBdate is 12/6/1944. I've entered 07251977, 12/26/1944, and more but it always says it's the same. What am I doing wrong?


    public static void main(String[] args) throws IOException {
    Scanner myScanner = new Scanner(System.in);
    Date myBdate = new Date();
    System.out.print("I was born on ");
    System.out.println(myBdate + ".");
    System.out.print("When were you born? ");
    birthDate2 = myScanner.nextInt();

    if (myBdate.compareTo(birthDate2) == 0)
    {
    System.out.println(myBdate + " and " + birthDate2 + " are the same.");
    }
    else if (myBdate.compareTo(birthDate2) > 0)
    {
    System.out.println(myBdate + " is older than " + birthDate2);
    }
    else if (myBdate.compareTo(birthDate2) < 0)
    {
    System.out.println(myBdate + " is newer than " + birthDate2);
    }


    Here's the full code for the program (other constructive criticism is welcome!):

    package tripObjects;
    import java.io.*;
    import java.util.InputMismatchException;
    import java.util.Scanner;
    public class Date {

    // field declarations for instance variables
    static int day;
    static int year;
    static int month;
    private static int birthDate2;

    // Constructor method
    public Date(int newDay, int newMonth, int newYear)
    {
    day = newDay; // constructors. // syntax is 'field declaration = parameters;'
    year = newYear;
    month = newMonth;

    }
    public Date() // default constructor method. This line is the start of an instance method
    {
    // constructors that initialize
    day = 6;
    year = 1944;
    month = 12;
    //day = getDay(); WHAT IS WRONG??
    //year = getYear();
    //month = getMonth();
    }
    // Methods. statements that say "return day,etc are the method bodies
    public int getDay()
    {
    return day;
    }
    public int getMonth()
    {
    return month;
    }
    public int getYear()
    {
    return year;
    }
    public String toString()
    {
    return month + "/" + day + "/" + year;
    }

    public void println(String toString)

    {}

    public String monthDate(String month)
    {
    StringBuffer outDate = new StringBuffer(month);
    outDate.append(" ");
    outDate.append(month);
    return outDate.toString();
    }
    public String dayDate(String day)
    {
    StringBuffer outDate = new StringBuffer(day);
    outDate.append(" ");
    outDate.append(day);
    return outDate.toString();
    }
    public String yearDate(String year)
    {
    StringBuffer outDate = new StringBuffer(year);
    outDate.append(" ");
    outDate.append(year);

    return outDate.toString();
    }
    public int compareTo(int birthDate2) {
    // TODO Auto-generated method stub
    return 0;
    }
    public static void main(String[] args) throws IOException {
    Scanner myScanner = new Scanner(System.in);
    Date myBdate = new Date();
    System.out.print("I was born on ");
    System.out.println(myBdate + ".");
    System.out.print("When were you born? ");
    birthDate2 = myScanner.nextInt();

    if (myBdate.compareTo(birthDate2) == 0)
    {
    System.out.println(myBdate + " and " + birthDate2 + " are the same.");
    }
    else if (myBdate.compareTo(birthDate2) > 0)
    {
    System.out.println(myBdate + " is older than " + birthDate2);
    }
    else if (myBdate.compareTo(birthDate2) < 0)
    {
    System.out.println(myBdate + " is newer than " + birthDate2);
    }


    }

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Uhhh... your code doesn't even compile. You cannot use Date.compareTo(int) as it does not accept an int parameter (which is what birthDate2 is). Your compiler may just be ignoring this error... either way, you need to either convert your Date object to an int, or your int object to a Date to compare them.

    Date (Java 2 Platform SE v1.4.2)

  3. #3
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    Java Code:
    // field declarations for instance variables
    static int day;
    static int year;
    static int month;
    private static int birthDate2;
    You've declared birthDate2 as an integer and the Date.compareTo method compares two Date types.


    Date (Java Platform SE 6)

    Hope this helps! :)
    Chris

    ----> Oh, ummmm, yeah what Zack said :) <-------
    Last edited by tashimoto; 11-05-2010 at 06:14 PM. Reason: responded at same time :)

  4. #4
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Don't know if tashimoto and Zack saw that you made your own Date class which you are using...

    Your compareTo(int) method inside your Date class is set to return 0 always. Put logic in there...

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Ah, geez. I saw that compareTo method but didn't realize it was in the Date class.

    @ OP, you should NEVER name your classes the same as Java API classes. Ever. ;) A good way to avoid this is to start classes with a specified prefix like myProj_Date or to just CamelCase it and start it with a lower letter (dateClass).

  6. #6
    tashimoto is offline Member
    Join Date
    Sep 2010
    Location
    Oregon, usa
    Posts
    69
    Rep Power
    0

    Default

    Ooops, I didn't see that, either. Thanks StormyWaters! :)

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,344
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Zack View Post
    Ah, geez. I saw that compareTo method but didn't realize it was in the Date class.
    Yes, and as already noted, it always returns zero (0) so that explains the resulting output. Also, that Date class doesn't implement the Comparable<Date> interface (and this overlayed compareTo( ... ) method doesn't implement it).

    kind regards,

    Jos

Similar Threads

  1. need help wit this compareTo method
    By ShinTec in forum New To Java
    Replies: 4
    Last Post: 02-02-2010, 02:24 AM
  2. Help with interfaces, compareTo etc
    By jigglywiggly in forum New To Java
    Replies: 10
    Last Post: 09-07-2009, 01:52 AM
  3. [SOLVED] [newbie] compareTo that returns an Enum
    By jon80 in forum New To Java
    Replies: 4
    Last Post: 05-17-2009, 11:03 PM
  4. compareTo()
    By Tsiliadoros in forum Advanced Java
    Replies: 5
    Last Post: 10-03-2008, 01:18 PM
  5. how compareTo Method works
    By nanaji in forum Advanced Java
    Replies: 1
    Last Post: 06-22-2008, 07:40 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •