File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How can I fix my code and print it properly in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can I fix my code and print it properly in java?" Watch "How can I fix my code and print it properly in java?" New topic
Author

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

Andres Soto
Greenhorn

Joined: Nov 03, 2012
Posts: 11
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

Joined: Feb 02, 2010
Posts: 42
You should use SimpleDateFormat

something like this to format your date format



Hope this will help
Red Smith
Ranch Hand

Joined: Aug 05, 2007
Posts: 136
    
    1
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

Joined: Nov 03, 2012
Posts: 11
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

Joined: Aug 16, 2005
Posts: 14424
    
  23

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.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I fix my code and print it properly in java?