Connecting Tech Pros Worldwide Forums | Help | Site Map

Days in a month

Newbie
 
Join Date: Mar 2007
Posts: 26
#1: May 25 '07
I'm using Netbeans as a complier.
I have to work on a project where a user can determine how many days are in each month in a particular year.
I already know how to prompt the user to enter their choices, but I'm feeling a little confused on how to set up the calculations.
I would assume I would have to set the months in a string? each month would have to be an int?
and I'm thinking I'll have to use a lot of if, else if statements too.

Does this make sense to anyone? I am not asking for anyone to do my homework, that is unethical, but any imput, tips, advice would be most welcomed.
Thank you.

dmjpro's Avatar
Lives Here
 
Join Date: Jan 2007
Location: India (West-Bengal)
Posts: 2,451
#2: May 25 '07

re: Days in a month


do u know GregorianCalendar????

kind regards.
dmjpro.
Newbie
 
Join Date: May 2007
Location: India
Posts: 3
#3: May 25 '07

re: Days in a month


Hi There,
I am writing the code that would solve ur problem :

<Code removed by admin>
prometheuzz's Avatar
Editor
 
Join Date: Apr 2007
Location: Rotterdam, the Netherlands.
Posts: 189
#4: May 25 '07

re: Days in a month


Quote:

Originally Posted by rents

I'm using Netbeans as a complier.

<nitpickymode>
Nebeans is not a compiler: it's an IDE.
</nitpickymode>


Quote:

Originally Posted by rents

...

Does this make sense to anyone? I am not asking for anyone to do my homework, that is unethical, but any imput, tips, advice would be most welcomed.
Thank you.

Yes, it makes sense. You already have the most difficult part of the assignment done: the input-part. So here's a hint as to how you could finish it:
Expand|Select|Wrap|Line Numbers
  1. public class Main {
  2.  
  3.     public static void main(String[] args) {
  4.         int y = /* your input method */
  5.         int m = /* your input method */
  6.         int d = getDays(year, month);
  7.         System.out.println("Month "+m+" from year "+y+" has "+d+" days.");
  8.     }
  9.  
  10.     private static int getDays(int year, int month) {
  11.         /*
  12.         what to return if 'month' is 1, 3, 5, 7, 8, 10 or 12?
  13.         what to return if 'month' is 4, 6, 9 or 11?
  14.         what to return if 'month' is 2 AND 'year' is a leap year?
  15.         what to return if 'month' is 2 AND 'year' is NOT a leap year?
  16.         */
  17.     }
  18.  
  19.     private static boolean isLeap(int year) {
  20.         /*
  21.         if 'year' is a leap year, return true, otherwise, return false
  22.         */
  23.     }
  24. }
nomad's Avatar
Expert
 
Join Date: Mar 2007
Location: CA.
Posts: 570
#5: May 25 '07

re: Days in a month


Quote:

Originally Posted by rents

I'm using Netbeans as a complier.
I have to work on a project where a user can determine how many days are in each month in a particular year.
I already know how to prompt the user to enter their choices, but I'm feeling a little confused on how to set up the calculations.
I would assume I would have to set the months in a string? each month would have to be an int?
and I'm thinking I'll have to use a lot of if, else if statements too.

Does this make sense to anyone? I am not asking for anyone to do my homework, that is unethical, but any imput, tips, advice would be most welcomed.
Thank you.

You could use a switch (operation) statement with a break
in which you know there are a certain months that have 31 days in it, and you know there are certain months that have 30 days. You know Feb can contain a leap years so you will need a calucation for that.
remember the year must be evenly divisible by 4 and not evenly divisible by 100
and the year must be evenly divisible by 400. So there will be two output for this one using an if; else; if statement. That's a big clue.
I was going to give you the cal, but that will not help you. look at the text I gave you. You should be able to figure it out.

Of course you will need to write a argument for lenght that is month and year...

nomad
prometheuzz's Avatar
Editor
 
Join Date: Apr 2007
Location: Rotterdam, the Netherlands.
Posts: 189
#6: May 25 '07

re: Days in a month


Quote:

Originally Posted by nomad

...
remember the year must be evenly divisible by 4 and not evenly divisible by 100
and the year must be evenly divisible by 400.
...

You probably mean "or" instead of "and":
a year is a leap year if it's [divisable by 4 AND NOT divisable by 100] OR [if it's divisable by 400].
nomad's Avatar
Expert
 
Join Date: Mar 2007
Location: CA.
Posts: 570
#7: May 25 '07

re: Days in a month


Quote:

Originally Posted by prometheuzz

You probably mean "or" instead of "and":
a year is a leap year if it's [divisable by 4 AND NOT divisable by 100] OR [if it's divisable by 400].

Yes thanks for the correction...

nomad
Newbie
 
Join Date: Mar 2007
Posts: 26
#8: May 25 '07

re: Days in a month


Thanks for all the responses. Sorry for saying netbeans was a complier, I should have known better..but it was late and I was exhausted :)

I'm using switch and it doesn't seem to be that difficult to figre out.
I had a question though on how to format my output statement..here is what I have

System.out.println ("\n\n The number of days in" + month "/" + year" is:" + days") ");

but I keep getting an error message on this line. I'm just having trouble typing out what it should say
blazedaces's Avatar
Needs Regular Fix
 
Join Date: May 2007
Posts: 280
#9: May 25 '07

re: Days in a month


Quote:

Originally Posted by rents

Thanks for all the responses. Sorry for saying netbeans was a complier, I should have known better..but it was late and I was exhausted :)

I'm using switch and it doesn't seem to be that difficult to figre out.
I had a question though on how to format my output statement..here is what I have

System.out.println ("\n\n The number of days in" + month "/" + year" is:" + days") ");

but I keep getting an error message on this line. I'm just having trouble typing out what it should say

You're forgetting a plus sign in between month and "/", also make sure there is a plus sign between EVERY string and variable and whatever else is in the parenthesis.

-blazed
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#10: May 25 '07

re: Days in a month


Quote:

Originally Posted by blazedaces

You're forgetting a plus sign in between month and "/", also make sure there is a plus sign between EVERY string and variable and whatever else is in the parenthesis.

-blazed

And that last trailing ")" thingie should be removed too.

kind regards,

Jos
Newbie
 
Join Date: Mar 2007
Posts: 26
#11: May 25 '07

re: Days in a month


gents,

here is what I have so far...however, I still have some questions.
The part where it asks for the month and the year work fine, but then nothing else happens after that. Am I missing some calculations? I'm just confused as to how to get to where I need to be. (the problem is outlines in my first post on this thread)
Expand|Select|Wrap|Line Numbers
  1. //Declare Variables
  2. ...              
  3. //Print heading
  4. ...
  5. //Request User Input
  6. System.out.printf ("\n\n Enter the year ");
  7. intyear = stream.nextInt();
  8. System.out.printf  ("\n\n Enter the month ");
  9. intmonth = stream.nextInt();
  10.  
  11. switch (intmonth) {
  12.       case 1:
  13.       case 3:
  14.       case 5:
  15.       case 7:
  16.       case 8:
  17.       case 10:
  18.       case 12:
  19.           intdays = 31;
  20.           break;
  21.        case 4:
  22.        case 6:
  23.        case 9:
  24.        case 11:
  25.            intdays = 30;
  26.            break;
  27.  
  28.         case 2:
  29.            System.out.println ("Checking for leap year (yes/no)");             
  30.            isLeapYear = consolein.nextBoolean();
  31.  
  32.            if (isLeapYear) {
  33.                 intdays = 29;
  34.            }  else {
  35.                 intdays = 28;
  36.            }    
  37.  
  38.           System.out.println ("The number of days in" + intmonth + "/" + intyear +" is:");
  39.                 }
  40.  
  41.  
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,393
#12: May 25 '07

re: Days in a month


Quote:

Originally Posted by rents

gents,

here is what I have so far...however, I still have some questions.
The part where it asks for the month and the year work fine, but then nothing else happens after that. Am I missing some calculations? I'm just confused as to how to get to where I need to be. (the problem is outlines in my first post on this thread)

Expand|Select|Wrap|Line Numbers
  1.         //Declare Variables
  2.         ...              
  3.         //Print heading
  4.         ...
  5.         //Request User Input
  6.         System.out.printf ("\n\n Enter the year ");
  7.         intyear = stream.nextInt();
  8.         System.out.printf  ("\n\n Enter the month ");
  9.         intmonth = stream.nextInt();
  10.  
  11.         switch (intmonth) {
  12.             case 1:
  13.             case 3:
  14.             case 5:
  15.             case 7:
  16.             case 8:
  17.             case 10:
  18.             case 12:
  19.                 intdays = 31;
  20.                         break;
  21.             case 4:
  22.             case 6:
  23.             case 9:
  24.             case 11:
  25.                 intdays = 30;
  26.                         break;
  27.  
  28.             case 2:
  29.                 System.out.println ("Checking for leap year (yes/no)");             
  30.                 isLeapYear = consolein.nextBoolean();
  31.  
  32.                 if (isLeapYear) {
  33.                     intdays = 29;
  34.                 }  else {
  35.                     intdays = 28;
  36.                 }    
  37.  
  38.                 System.out.println ("The number of days in" + intmonth + "/" + intyear +" is:");
  39.                 }
  40.  
  41.         }

There are quite a few problems with this but what chiefly catches my eye is:
Expand|Select|Wrap|Line Numbers
  1.     System.out.println ("The number of days in" + intmonth + "/" + intyear +" is:");
Do you see anything wrong with this?
nomad's Avatar
Expert
 
Join Date: Mar 2007
Location: CA.
Posts: 570
#13: May 25 '07

re: Days in a month


Expand|Select|Wrap|Line Numbers
  1.  isLeapYear = consolein.nextBoolean();
  2.  
  3.                 if (isLeapYear) {
  4.                     intdays = 29;
  5.                 } else {
  6.                     intdays = 28;
  7.                 }    
  8.  

this also caught my eyes. There is no test going on.
nomad
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,393
#14: May 25 '07

re: Days in a month


Quote:

Originally Posted by nomad

Expand|Select|Wrap|Line Numbers
  1.  isLeapYear = consolein.nextBoolean();
  2.  
  3.                 if (isLeapYear) {
  4.                     intdays = 29;
  5.                 } else {
  6.                     intdays = 28;
  7.                 }    
  8.  

this also caught my eyes. There is no test going on.
nomad

There is a test, it tests if the user entered in a true value for leap year or a false value for not a leap year. I don't know if I would be very happy if I had to tell the computer if it was a leap year or not. Seems like something a computer could calculate very quickly whereas I would have to look it up.
nomad's Avatar
Expert
 
Join Date: Mar 2007
Location: CA.
Posts: 570
#15: May 25 '07

re: Days in a month


IF some one types in feb how does it know if there are 28 days or 29?
can you please explain please.
thanks
nomad
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,393
#16: May 25 '07

re: Days in a month


Quote:

Originally Posted by nomad

Also I see another problem here

Expand|Select|Wrap|Line Numbers
  1.  
  2. isLeapYear = consolein.nextBoolean();
  3.  
  4.                 if (isLeapYear) {
  5.                     intdays = 29;
  6.                 }  else {
  7.                     intdays = 28;
  8.                 }    
  9.  
  10.  
There is no test going how if some type in feb what will happen.

nomad

Didn't you just post this?

EDIT: Nomad keeps editing his posts so now this one doesn't make any sense. Disregard it.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#17: May 25 '07

re: Days in a month


I agree with dmjpro (see reply#2): why not instantiate a GregorianCalendar
given any day in a month and a given year. The getActualMaximum() gives
you the number of days in that month in that year.

kind regards,

Jos
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,393
#18: May 25 '07

re: Days in a month


Quote:

Originally Posted by JosAH

I agree with dmjpro (see reply#2): why not instantiate a GregorianCalendar
given any day in a month and a given year. The getActualMaximum() gives
you the number of days in that month in that year.

kind regards,

Jos

Jos you silly, silly boy. Using a GregorianCalendar would be too easy. Besides what if he wants to eventually accept input from the user in sidereal time?

Yes kids, its called sarcasm.
nomad's Avatar
Expert
 
Join Date: Mar 2007
Location: CA.
Posts: 570
#19: May 25 '07

re: Days in a month


Quote:

Originally Posted by RedSon

Didn't you just post this?

EDIT: Nomad keeps editing his posts so now this one doesn't make any sense. Disregard it.

Sorry about that one RedSon.
I read and I saw a mistake... and I wanted to correct it. You're to fast on the draw today.

nomad
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#20: May 25 '07

re: Days in a month


Quote:

Originally Posted by RedSon

Jos you silly, silly boy. Using a GregorianCalendar would be too easy. Besides what if he wants to eventually accept input from the user in sidereal time?

Yes kids, its called sarcasm.

I have just one grown-up answer to that: :-P <kick-dust> So there! Duh!

kind regards,

Jos ;-)
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,393
#21: May 25 '07

re: Days in a month


Quote:

Originally Posted by nomad

Sorry about that one RedSon.
I read and I saw a mistake... and I wanted to correct it. You're to fast on the draw today.

nomad

Huh, my wife complains about that too.

Okay no more hijacking threads after this one.
Newbie
 
Join Date: Mar 2007
Posts: 26
#22: May 25 '07

re: Days in a month


Quote:

Originally Posted by RedSon

There are quite a few problems with this but what chiefly catches my eye is:

Expand|Select|Wrap|Line Numbers
  1.     System.out.println ("The number of days in" + intmonth + "/" + intyear +" is:");
Do you see anything wrong with this?


is it too many +'s or the fact that I'm using intmonth? intyear? should it just be 'month' 'year'?
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,393
#23: May 25 '07

re: Days in a month


Quote:

Originally Posted by rents

is it too many +'s or the fact that I'm using intmonth? intyear? should it just be 'month' 'year'?

its not about intmonth or intyear, although those are probably poor choices for variable names. It has to do with what comes after the ":".
nomad's Avatar
Expert
 
Join Date: Mar 2007
Location: CA.
Posts: 570
#24: May 25 '07

re: Days in a month


Quote:

Originally Posted by rents

is it too many +'s or the fact that I'm using intmonth? intyear? should it just be 'month' 'year'?

You just want to print out the amount of days in a giving month right!!!!
SOooo!!!
nomad
Newbie
 
Join Date: Mar 2007
Posts: 26
#25: May 26 '07

re: Days in a month


ahh crap..duhhhh. I am really dumb with this stuff.
I'm a complete novice and I think I make a bigger deal than I should out of this.
Newbie
 
Join Date: Mar 2007
Posts: 26
#26: May 26 '07

re: Days in a month


ok, here is what I have so far. When I run the program, it asks you to enter the year and the month, and after that, nothing happens. What am I missing here in order to figure out the days? I can't think of what to do..

/*
* Main.java
*
* Created on May 26, 2007, 8:29 AM
*
*
*
*

*/




package daysinamonthcalculator;
import java.util.Scanner;
/**
*
*
*/
public class Main {

/** Creates a new instance of Main */
public Main() {
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
//Declare Variables
int intyear = 1;
int intmonth = 1;
int intdays = 1;
boolean isLeapYear;
Scanner consolein = new Scanner(System.in);




//Request User Input
System.out.printf ("\n\n Enter the year ");
intyear = consolein.nextInt();
System.out.printf ("\n\n Enter the month ");
intmonth = consolein.nextInt();


switch (intmonth) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
intdays = 31;
break;
case 4:
case 6:
case 9:
case 11:
intdays = 30;
break;

case 2:
//Check for leap year

System.out.println ("Checking for leap year (true/false)");
isLeapYear = consolein.nextBoolean();

isLeapYear =
((intyear % 4 == 0 && intyear % 100 != 0) || (intyear % 400 == 0));


if (isLeapYear) {
intdays = 29;
} else {
intdays = 28; }


//Display results
System.out.println ("The number of days in" + intmonth + "/" + intyear +" is:" + intdays + "");
System.out.print ("Thank you. Run Completed");



}

}
}
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#27: May 26 '07

re: Days in a month


Erm, <raises hand/>, hint: it starts with a capital G and ends with "regorianCalendar".

kind regards,

Jos ;-)
RedSon's Avatar
Site Moderator
 
Join Date: Jan 2007
Location: America
Posts: 3,393
#28: May 27 '07

re: Days in a month


i think you should double check the position of your brackets

and you need to use code tags or i'm going to start getting cranky!
Member
 
Join Date: Mar 2007
Posts: 46
#29: May 29 '07

re: Days in a month


You were doing just fine until switch had to check for leap year. You seem nervous there. in life as well as in coding java, remember, KISS. Why did you have to check for nextBoolean when the user doesn’t know if it’s a leap year or not? Doesn’t make sense. That’s the work of your program. Take away consolein.nextBoolean from the logic of your code. After that line, you need to realize that you’re checking intyear (according to convention it should be intYear, pls!) for leap year where switch evaluates to 2. get it. So, what if we want to check for leap year? Keeping it simple, I’ve prepared something based on the principles below.
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. public class Checker {
  4.     public static void main(String[] args) {
  5.         int intyear = 1996;  //assuming this is what the user entered.
  6.         int intdays;  //you have this already from the user
  7.  
  8.         //the below if from your logic for checking leap years. don't have
  9.         //the time to consult the wikipedia. 
  10.         <Line removed to comply with homework posting guidelines>
  11.  
  12.         System.out.println("The number of days if february is chosen for "+
  13.                 intyear+" is "+intdays);
  14.     }
  15. }
  16. //this code is based on principle i.e where your switch evaluates to 2. 
now your leap year is resolved I believe.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#30: May 29 '07

re: Days in a month


<desparately trembling voice>
GregorianCalendar!for!Pete's!sake!
Are!you!all!deaf!or!something?!
<desparately trembling voice/>

kind regards,

Jos ;-)
prometheuzz's Avatar
Editor
 
Join Date: Apr 2007
Location: Rotterdam, the Netherlands.
Posts: 189
#31: May 29 '07

re: Days in a month


Quote:

Originally Posted by JosAH

<desparately trembling voice>
GregorianCalendar!for!Pete's!sake!
Are!you!all!deaf!or!something?!
<desparately trembling voice/>

kind regards,

Jos ;-)

How many crates of Grolsch are we going to bet that the OP cannot make use of the classes from the java.util package in order to fulfill this assign... , err, task?
; )
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#32: May 29 '07

re: Days in a month


Quote:

Originally Posted by prometheuzz

How many crates of Grolsch are we going to bet that the OP cannot make use of the classes from the java.util package in order to fulfill this assign... , err, task?
; )

I don't know what Grolsch is but I suspect it's some alcoholic fluid capable of damaging the lungs. Such substances can be harmful to stu..., err members and should not be mentioned in the tech forums.

ADMIN.
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#33: May 29 '07

re: Days in a month


Quote:

Originally Posted by r035198x

I don't know what Grolsch is but I suspect it's some alcoholic fluid capable of damaging the lungs. Such substances can be harmful to stu..., err members and should not be mentioned in the tech forums.

ADMIN.

Unless you were talking about empty crates of course ...
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#34: May 29 '07

re: Days in a month


Quote:

Originally Posted by r035198x

Unless you were talking about empty crates of course ...

<shudder/> *empty crates!* oh, the horror!

kind regards,

Jos ;-)
Newbie
 
Join Date: May 2007
Posts: 2
#35: May 29 '07

re: Days in a month


You could possibly look at the Calendar object, it offers get and and set methods that would allow you do what you wanted. No point re-inventing wheel.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#36: May 29 '07

re: Days in a month


Quote:

Originally Posted by tommybobby

You could possibly look at the Calendar object, it offers get and and set methods that would allow you do what you wanted. No point re-inventing wheel.

Thank you, thank!you!thank!you! kiss!kiss!kiss!

kind regards,

Jos

ps. finally somebody understands! ;-)
Reply