Results 1 to 9 of 9
- 11-04-2012, 03:43 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 30
- Rep Power
- 0
Trouble printing out the date for a method
This is the class that's not main
This is my tester classJava Code:/** * A class to give students experience using loops. This class creates and * manipulates objects of Greg's Date class. */ public class SpeedDating { // Note: this class has no instance variables! /** * Creates an empty SpeedDating object so that you can call the methods (a * constructor that takes no parameters is known as a "default" constructor) */ public SpeedDating() { } // Constructor has empty body /** * Prints the day of the week (e.g. "Thursday") on which Halloween will fall * for 10 consecutive years. * * @param startYear the first of the 10 consecutive years */ public void printHalloweens(int startYear) { int lastYear = startYear + 10; for (int i = startYear; i < lastYear; i++) { Date Halloween = new Date(10, 31, i); System.out.print(Halloween.getYear() + ", "); System.out.println(Halloween.getDayOfWeek() + ", "); } // TO DO: write body of this method here } // TO DO: write body of this method here /** * Computes and returns the Date on which Thanksgiving will fall in a given * year. * * NOTE: By law, Thanksgiving is the 4th Thursday in November * * @param year the year for which to compute the date of Thanksgiving * @return the Date of Thanksgiving for the specified year */ public Date getThanksgiving(int year) { Date Thanksgiving = new Date(11, 1, year); int count = 1; int weekCount = 0; for ( ; count <= 30; count++) { if (Thanksgiving.getDayOfWeek().equals("Thursday")) { weekCount++; } if (weekCount == 4) { break; } Thanksgiving.next(); } return Thanksgiving; } // TO DO: write body of this method here /** * Computes and returns the number of days between two dates, counting the * end date but not the start date. E.g., the number of days between * 11/1/2012 and 11/5/2012 is 4, not 5. * * Precondition: The start date must occur on or before the end date. * * @param start the earlier of the two dates * @param end the later of the two dates * * @return the number of days elapsed between the start date and the end * date */ public int countingTheDays(Date start, Date end) { do{ start.next(); }while(start.getMonth()>end.getMonth() && start.getDay() < end.getDay()); return start.getDay(); // TO DO: write body of this method here } }
The problem is that I can't seem to print out the date. I get something like this Date@f449b8. What's going on? How can I fix it?Java Code:import javax.swing.JOptionPane; /* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author */ public class SpeedDatingTester { public static void main (String args[]) { SpeedDating date=new SpeedDating(); String input = JOptionPane.showInputDialog ("Enter the halloween year ") ; int startYear = Integer.parseInt(input) ; date.printHalloweens(startYear); input = JOptionPane.showInputDialog ("Enter the year for november ") ; int novyear = Integer.parseInt(input) ; date.getThanksgiving(novyear) ; Date Thanksgiving = date.getThanksgiving(novyear) ; System.out.println(Thanksgiving) ;Last edited by Michael305rodri; 11-04-2012 at 04:11 AM.
- 11-04-2012, 03:46 AM #2
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Trouble printing out the date for a method
See SimpleDateFormat (Java Platform SE 7 ) to format and print your Date object.
ex.
Java Code:SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy"); System.out.print(sdf.format(new Date()));Last edited by SJF; 11-04-2012 at 03:50 AM. Reason: example
- 11-04-2012, 05:15 AM #3
Member
- Join Date
- Oct 2012
- Posts
- 30
- Rep Power
- 0
Re: Trouble printing out the date for a method
I'm still lost :(
I now changed the code to this
SpeedDating thanksgiving = new SpeedDating() ;
input = JOptionPane.showInputDialog
("Enter the year for november ") ;
int year = Integer.parseInt(input) ;
System.out.println(thanksgiving.getThanksgiving(ye ar)) ;
}
}
But it printed out Date@1e91a4d
- 11-04-2012, 05:54 AM #4
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Trouble printing out the date for a method
You need to put the Date() object that is being returned from thanksgiving.getThanksgiving(year) into SimpleDateFormat.
What is currently being printed out is the default Object.toString() because Date() does not have an override for .toString() Java is telling you that it is a Date object at memory reference 1e91a4d.... Not very useful unless you know how to read memory at 1e91a4d and turn the bits there into a human readable time.... oh! wait! SimpleDateFormat can do that for you!
- 11-04-2012, 06:25 AM #5
Member
- Join Date
- Oct 2012
- Posts
- 30
- Rep Power
- 0
Re: Trouble printing out the date for a method
Thank you. I now understand what's going on. The memory reference and all that you said is very similar to what my professor said. I'm still having trouble writing the code. So I import java.text.SimpleDateFormat; ? And SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy"); is kind of throwing me off. The parameters would be the day, month, and year? So I put the month and days as ints and for year i just put year because that's what the user has to input. I'm sorry... Is that I'm very new to programming :(
- 11-04-2012, 06:49 AM #6
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: Trouble printing out the date for a method
So you'd create the SimpleDateFormat object with a format string in "EEE, d MMM yyyy" is 'Day name in week ', 'Day' 'Mon' 'Year' but look at the link above for other formats... like "dd/MM/yyyy"
Then give the Date object that getThanksgiving(year) is providing to the SimpleDateFormat object
Java Code:String tdayStr = sdf.format(thanksgiving.getThanksgiving(year)); // This is the string representation of the date System.out.println(tdayStr);
- 11-04-2012, 07:09 AM #7
Member
- Join Date
- Oct 2012
- Posts
- 30
- Rep Power
- 0
Re: Trouble printing out the date for a method
I went to the site but I don't think they have that format :( The closest one is "EEE, MMM d, ''yy" thank you though I feel like I'm getting really close!
I tried this:
However it's giving me an error message. Cannot find symbol Symbol: method format(Date) location : class SpeedDatingJava Code:String thanksdate = SpeedDating.format(thanksgiving.getThanksgiving(year)); System.out.println(thanksdate);
- 11-05-2012, 12:21 AM #8
Member
- Join Date
- Oct 2012
- Posts
- 30
- Rep Power
- 0
Re: Trouble printing out the date for a method
Please someone help. My assignment is due soon and I haven't been able to print the thanksgiving method correctly yet :(
- 11-05-2012, 11:17 AM #9
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Similar Threads
-
Having trouble printing the LinkedList correctly..
By teekei in forum New To JavaReplies: 18Last Post: 08-09-2011, 08:08 AM -
Is their any method which converts a Julian date to an actual date.
By renu in forum New To JavaReplies: 2Last Post: 05-12-2011, 04:52 PM -
Having trouble with printing a blank line.
By Meta in forum New To JavaReplies: 4Last Post: 05-11-2010, 10:54 PM -
Having trouble looping & printing output
By carrotcake in forum New To JavaReplies: 1Last Post: 04-04-2010, 05:37 AM -
Having printing trouble
By random0munky in forum New To JavaReplies: 5Last Post: 12-07-2009, 09:27 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks