It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Finding number of days in a month confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Finding number of days in a month confusion" Watch "Finding number of days in a month confusion" New topic
Author

Finding number of days in a month confusion

Mezan Shareef
Ranch Hand

Joined: Apr 01, 2010
Posts: 40
To write a program to prompt the user to enter month and year, and display number of days in a month. for example if user entered month 2 and year 2000 the program should display that Feb 2000 has 29 days. if user enters month 3 year 2005, program should display Mar 2005 has 31 days.

I am unable to strategize my mind, i am stuck i did little bit, how can i do the rest part. please help.

Jean Fontaine
Greenhorn

Joined: Apr 12, 2011
Posts: 12
Hi.
Well, at first you should make a switch to say : case 1 : month = "JAN"
case 2 : month = "FEB" ...
Then say if (month.equals("JAN") || month.equals("MAR") ...)
{ days = 31; }
else if (month.equals("APR") || month.equals("JUN")...
{days = 30 }

gotcha ?
Mezan Shareef
Ranch Hand

Joined: Apr 01, 2010
Posts: 40
I did what you said jean it throws 2 errors for the changes you said.

Type incompatible for case change you said.

int can not be dereferenced for the if statement change.

can you please again think. I am shut off. This is why it becomes tough for me to learn java. i always get stuck so that, i can not recover from errors... please help me guys, this time i do not want to quit while learning this dirty java.

Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Do you keep leap years in mind? Cause your solution now doesn't. I'll give you a hint: Calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
And DateFormat can provide the month names.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Jean Fontaine
Greenhorn

Joined: Apr 12, 2011
Posts: 12
Hmmm...
It's because month is an int.
You have t make a Stirng named monthName, then say :

case 1 : monthName = "JAN" etc..
Then it should do.

edit : oh and replace if month.equals() by if monthName.equals().
another edit : also, instead of
if { }
if {}
if { },
say
if { }
else if { }
else if { }

You also have to initialize your String by saying String monthName = ""

LAST EDIT : WORKS FOR ME !

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
Another possibility:This is a very basic and rudimentary block, which will require a lot of refinement. And remember that inJavaâ„¢ January = 0 and December = 11.
Mezan Shareef
Ranch Hand

Joined: Apr 01, 2010
Posts: 40
THank you Guys, !! Your sugestions helped. IT also worked for me.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

What did you end up using?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
Wouter Oet wrote:What did you end up using?
Whichever you used: "You're welcome"
Jean Fontaine
Greenhorn

Joined: Apr 12, 2011
Posts: 12
No problem !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Finding number of days in a month confusion
 
Similar Threads
What is dom?
2 Dimensional Arrays / Loop problem....
Date Function
Do not know how to code some cases and exit the loop with "Enter"
help with Progamme