This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with Beginner java code

 
joe burke
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys i am currently taking a java class and have never programmed before. i need to write a program that takes 3 parameters (month, day, year). Then outputs the day of the week, the date, whether or not the year is a leap year and the number of days left in the year. I have everything working except for the day of the week and the number of days left in the year.
Here is my code:


i would really appreciate if someone can help me out.
Thank You.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch. Good start ! You should have a look at SimpleDateFormat too, if you are allowed to use it.

Why are you stuck by the day of the week ? Did you look at the Calendar API carefully ?

(your class name should start with an uppercase letter)
 
joe burke
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:Welcome to the ranch. Good start ! You should have a look at SimpleDateFormat too, if you are allowed to use it.

Why are you stuck by the day of the week ? Did you look at the Calendar API carefully ?

(your class name should start with an uppercase letter)


I tried looking at it but i dont think that i need that, i tried but it gave me errors. and im stuck at the day of the week because its supposed to output a number 0 = sunday and so on. But for some reason its starting the week on a random day which is different for every month. I looked over the Calendar and Gregorian Calendar APIs multiple times and tried to even set a start day for the week but none of it seems to be working.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But for some reason its starting the week on a random day which is different for every month.

What do you mean ? If every month start with a different day, the result for Calendar.DAY_OF_WEEK will be different.
 
joe burke
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:
But for some reason its starting the week on a random day which is different for every month.

What do you mean ? If every month start with a different day, the result for Calendar.DAY_OF_WEEK will be different.

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Calendar.html#DAY_OF_WEEK
So for example if i input 09 28 2012 its should output Friday - September 28, 2012. for some reason isnt outputting the correct number. The program should be able to output the day of the week for any date entered
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using DAY_OF_WEEK_IN_MONTH ? DAY_OF_WEEK should do.
 
joe burke
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:Why are you using DAY_OF_WEEK_IN_MONTH ? DAY_OF_WEEK should do.

sorry i edited it, i didnt mean to use that, ive been staring at this since 9 and cant figure it out lol
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The calendar month starts at 0, but you are setting newCal.set(y, m, d); with m being the month (starting from 1 I suppose). I think this is the reason why you are getting the wrong DAY_OF_WEEK.
 
joe burke
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:The calendar month starts at 0, but you are setting newCal.set(y, m, d); with m being the month (starting from 1 I suppose). I think this is the reason why you are getting the wrong DAY_OF_WEEK.


Well the month is coming out correctly, but even if i change the order to (m, d, y) or any other order it works fine but the day of the week does not come out right. I pretty much need to set sunday to 0, monday to 1 and so on. So that i can use an array to output the actual day for any date the user inputs
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
even if i change the order to (m, d, y)

I was suggesting to set it to (y, m-1, d)

Can you tell me what are you application parameters, and which value does DAY_OF_WEEK return ?
 
joe burke
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:
even if i change the order to (m, d, y)

I was suggesting to set it to (y, m-1, d)

Can you tell me what are you application parameters, and which value does DAY_OF_WEEK return ?


The month outputs perfectly, and i subtract it by one when i output it already. Its the actual day of the week im having a problem outputting. So for example if the program is run: java leapYear 09 27 2012
its outputs: 6 for day of the week. If the day input is the 28th the day of the week outputs 1..
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I said, newCal.set(y, m, d) will create a calendar for the next month. October in your case. You have to substract one. You are getting the correct month because you explicity substract 1 from the result.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic