File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding number of days in a month confusion

 
Mezan Shareef
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jean Fontaine
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THank you Guys, !! Your sugestions helped. IT also worked for me.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you end up using?
 
Campbell Ritchie
Sheriff
Pie
Posts: 47300
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:What did you end up using?
Whichever you used: "You're welcome"
 
Jean Fontaine
Greenhorn
Posts: 12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem !
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic