• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Finding number of days in a month confusion  RSS feed

 
Ranch Hand
Posts: 40
  • 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.

 
Greenhorn
Posts: 12
  • 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
  • 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.

 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • 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
  • 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 !

 
Marshal
Posts: 61690
192
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THank you Guys, !! Your sugestions helped. IT also worked for me.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you end up using?
 
Campbell Ritchie
Marshal
Posts: 61690
192
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem !
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!