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

    Default Cant get past 1st if statement in driver

    I have an assignment to write a driver to test class Date, using December 6, 1944. This is what appears in the console when I run the application:
    I was born on 12/6/1944.
    When were you born? 12161944
    12/6/1944 and 12161944 are the same.

    No matter what date I enter, it displays, 12/6/1944 and [DATE I ENTER] are the same. It looks like its not evaluating past the the 1st if statement but I'm not sure why. Thanks in advance!
    Here is the driver:
    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 is the class:
    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 = 06; // default constructors
    year = 1944;
    month = 12;

    }
    // 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() // converts the date from integer to string. returns content in good format for output. new content can b directly output to print or println
    {
    return month + "/" + day + "/" + year;
    }

    public void println(String toString) // allows the content to be directly output to print or println

    {}

    public String monthDate(String month) // month is the obj, Date is the meth
    {
    StringBuffer outDate = new StringBuffer(month);
    outDate.append(" ");
    outDate.append(month);
    return outDate.toString();
    }
    public String dayDate(String day) // day is the obj, Date is the meth
    {
    StringBuffer outDate = new StringBuffer(day);
    outDate.append(" ");
    outDate.append(day);
    return outDate.toString();
    }
    public String yearDate(String year) // year is the obj, Date is the meth
    {
    StringBuffer outDate = new StringBuffer(year);
    outDate.append(" ");
    outDate.append(year);

    return outDate.toString();
    }
    public int compareTo(int birthDate2) {
    return 0;
    }

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

    Default

    Java Code:
    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);
    Java Code:
    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 = 06; // default constructors
       year = 1944;
       month = 12;
    
    }
    // 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() // converts the date from integer to string. returns content in good format for output. new content can b directly output to print or println
    {
       return month + "/" + day + "/" + year;
    }
    
    public void println(String toString) // allows the content to be directly output to print or println
    
    {}
    
    public String monthDate(String month) // month is the obj, Date is the meth
    {
       StringBuffer outDate = new StringBuffer(month);
       outDate.append(" ");
       outDate.append(month);
       return outDate.toString();
    }
    public String dayDate(String day) // day is the obj, Date is the meth
    {
       StringBuffer outDate = new StringBuffer(day);
       outDate.append(" ");
       outDate.append(day);
       return outDate.toString();
    }
    public String yearDate(String year) // year is the obj, Date is the meth
    {
       StringBuffer outDate = new StringBuffer(year);
       outDate.append(" ");
       outDate.append(year);
    
    return outDate.toString();
    }
       public int compareTo(int birthDate2) {
       return 0;
    }
    I see that you have some initial values in your Date class constructor... does this ever change?? Hint: when your user enters their birthdate date...

    Hope this helps!
    Chris
    Last edited by tashimoto; 12-03-2010 at 09:00 PM.

  3. #3
    berkeleybross's Avatar
    berkeleybross is offline Senior Member
    Join Date
    Feb 2010
    Location
    England
    Posts
    187
    Rep Power
    11

    Default

    Look at the method for compareTo...
    it always returns 0 and nothing else.

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

    Default

    Oh yea, my mistake... Ignore my post: except for the code tags - they make reading code much easier! :)

    Sorry!
    Chris

  5. #5
    erin.ctm is offline Member
    Join Date
    Oct 2010
    Posts
    10
    Rep Power
    0

    Default

    Thanks! I want to be able to enter in a birthdate then compare it to 12/06/1944. Should this line return something other than 0? This is where I'm confused.

    return outDate.toString();
    }
    public int compareTo(int birthDate2) {
    return 0;
    }

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

Similar Threads

  1. New To Java Ive Done Some Coding In The Past!
    By Noob Koer in forum New To Java
    Replies: 1
    Last Post: 07-08-2010, 07:46 AM
  2. [SOLVED] Cut,Past,copy in edit menu
    By smartsubroto in forum New To Java
    Replies: 9
    Last Post: 07-03-2008, 10:15 AM
  3. TextArea additable and uneditable (copy/past problem)
    By qwerty55 in forum Advanced Java
    Replies: 0
    Last Post: 01-19-2008, 11:41 PM
  4. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 04:45 PM
  5. Help with JDBC driver
    By Daniel in forum JDBC
    Replies: 2
    Last Post: 07-03-2007, 08:16 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
  •