Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I fix my code and print it properly in java?

 
Andres Soto
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Someone told me I have to use SimpleDateFormat
This is the loop that checks for the fourth thursday of november (thankgiving)


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;
}


}



This is the test class


SpeedDating thanksgiving = new SpeedDating() ;

input = JOptionPane.showInputDialog
("Enter the year for november ") ;

int year = Integer.parseInt(input) ;

System.out.println(thanksgiving.getThanksgiving(int year));


Basically the user inputs a year and my program supposed to print the date. For example, if the user inputs 2012 then my program will print out 11,22,2012. However my program is printing out something like Date@1ea0105 which someone told me was a reference (memory location). How can I fix this? Thank you.
 
echarish Kumar
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should use SimpleDateFormat

something like this to format your date format



Hope this will help
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to fix the line where you set the start date as the Date constructor takes different things than you think it does. Look at the detailed explanation for the constructor to get all the required information.


Also, in your loop, you are incrementing a counter for the week and for the day. So you will have ints that represent the week and the day and the year of Thanksgiving, But what will you return from the function?

The Date class does have a method to determine the day of the week, but it is not getDayOfWeek()
 
Andres Soto
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My professor said to use this.
d1.getShortDate() // returns the date as mm/dd/yyyy
How would I use that?
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15490
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are bugs in your code to calculate the thanksgiving date; it's not doing what you think it does.

See my remarks in your other topic.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
avogrado heisenburg wrote:My professor said to use this.
d1.getShortDate() // returns the date as mm/dd/yyyy
How would I use that?


Don't know. What type is d1? It's not a java.util.Date, as that class has no such method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic