Days in a month | Newbie | | Join Date: Mar 2007
Posts: 26
| | |
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.
|  | Lives Here | | Join Date: Jan 2007 Location: India (West-Bengal)
Posts: 2,451
| | | re: Days in a month
do u know GregorianCalendar????
kind regards.
dmjpro.
| | Newbie | | Join Date: May 2007 Location: India
Posts: 3
| | | re: Days in a month
Hi There,
I am writing the code that would solve ur problem :
<Code removed by admin>
|  | Editor | | Join Date: Apr 2007 Location: Rotterdam, the Netherlands.
Posts: 189
| | | 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: - public class Main {
-
-
public static void main(String[] args) {
-
int y = /* your input method */
-
int m = /* your input method */
-
int d = getDays(year, month);
-
System.out.println("Month "+m+" from year "+y+" has "+d+" days.");
-
}
-
-
private static int getDays(int year, int month) {
-
/*
-
what to return if 'month' is 1, 3, 5, 7, 8, 10 or 12?
-
what to return if 'month' is 4, 6, 9 or 11?
-
what to return if 'month' is 2 AND 'year' is a leap year?
-
what to return if 'month' is 2 AND 'year' is NOT a leap year?
-
*/
-
}
-
-
private static boolean isLeap(int year) {
-
/*
-
if 'year' is a leap year, return true, otherwise, return false
-
*/
-
}
-
}
|  | Expert | | Join Date: Mar 2007 Location: CA.
Posts: 570
| | | 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
|  | Editor | | Join Date: Apr 2007 Location: Rotterdam, the Netherlands.
Posts: 189
| | | 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].
|  | Expert | | Join Date: Mar 2007 Location: CA.
Posts: 570
| | | 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
| | | 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
|  | Needs Regular Fix | | Join Date: May 2007
Posts: 280
| | | 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
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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
| | | 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) -
//Declare Variables
-
...
-
//Print heading
-
...
-
//Request User Input
-
System.out.printf ("\n\n Enter the year ");
-
intyear = stream.nextInt();
-
System.out.printf ("\n\n Enter the month ");
-
intmonth = stream.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:
-
System.out.println ("Checking for leap year (yes/no)");
-
isLeapYear = consolein.nextBoolean();
-
-
if (isLeapYear) {
-
intdays = 29;
-
} else {
-
intdays = 28;
-
}
-
-
System.out.println ("The number of days in" + intmonth + "/" + intyear +" is:");
-
}
-
-
|  | Site Moderator | | Join Date: Jan 2007 Location: America
Posts: 3,393
| | | 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) -
//Declare Variables
-
...
-
//Print heading
-
...
-
//Request User Input
-
System.out.printf ("\n\n Enter the year ");
-
intyear = stream.nextInt();
-
System.out.printf ("\n\n Enter the month ");
-
intmonth = stream.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:
-
System.out.println ("Checking for leap year (yes/no)");
-
isLeapYear = consolein.nextBoolean();
-
-
if (isLeapYear) {
-
intdays = 29;
-
} else {
-
intdays = 28;
-
}
-
-
System.out.println ("The number of days in" + intmonth + "/" + intyear +" is:");
-
}
-
-
}
There are quite a few problems with this but what chiefly catches my eye is: -
System.out.println ("The number of days in" + intmonth + "/" + intyear +" is:");
Do you see anything wrong with this?
|  | Expert | | Join Date: Mar 2007 Location: CA.
Posts: 570
| | | re: Days in a month - isLeapYear = consolein.nextBoolean();
-
-
if (isLeapYear) {
-
intdays = 29;
-
} else {
-
intdays = 28;
-
}
-
this also caught my eyes. There is no test going on.
nomad
|  | Site Moderator | | Join Date: Jan 2007 Location: America
Posts: 3,393
| | | re: Days in a month Quote:
Originally Posted by nomad - isLeapYear = consolein.nextBoolean();
-
-
if (isLeapYear) {
-
intdays = 29;
-
} else {
-
intdays = 28;
-
}
-
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.
|  | Expert | | Join Date: Mar 2007 Location: CA.
Posts: 570
| | | 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
|  | Site Moderator | | Join Date: Jan 2007 Location: America
Posts: 3,393
| | | re: Days in a month Quote:
Originally Posted by nomad Also I see another problem here -
-
isLeapYear = consolein.nextBoolean();
-
-
if (isLeapYear) {
-
intdays = 29;
-
} else {
-
intdays = 28;
-
}
-
-
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.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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
|  | Site Moderator | | Join Date: Jan 2007 Location: America
Posts: 3,393
| | | 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.
|  | Expert | | Join Date: Mar 2007 Location: CA.
Posts: 570
| | | 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
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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 ;-)
|  | Site Moderator | | Join Date: Jan 2007 Location: America
Posts: 3,393
| | | 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
| | | 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: -
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'?
|  | Site Moderator | | Join Date: Jan 2007 Location: America
Posts: 3,393
| | | 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 ":".
|  | Expert | | Join Date: Mar 2007 Location: CA.
Posts: 570
| | | 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
| | | 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
| | | 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");
}
}
}
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | re: Days in a month
Erm, <raises hand/>, hint: it starts with a capital G and ends with "regorianCalendar".
kind regards,
Jos ;-)
|  | Site Moderator | | Join Date: Jan 2007 Location: America
Posts: 3,393
| | | 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
| | | 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. -
-
-
public class Checker {
-
public static void main(String[] args) {
-
int intyear = 1996; //assuming this is what the user entered.
-
int intdays; //you have this already from the user
-
-
//the below if from your logic for checking leap years. don't have
-
//the time to consult the wikipedia.
-
<Line removed to comply with homework posting guidelines>
-
-
System.out.println("The number of days if february is chosen for "+
-
intyear+" is "+intdays);
-
}
-
}
-
//this code is based on principle i.e where your switch evaluates to 2.
now your leap year is resolved I believe.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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 ;-)
|  | Editor | | Join Date: Apr 2007 Location: Rotterdam, the Netherlands.
Posts: 189
| | | 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
| | | 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
| | | 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 ...
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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
| | | 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.
|  | Expert | | Join Date: Mar 2007
Posts: 10,611
| | | 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! ;-)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|