# calculate the number of days in a month

Page 1 of 1

## 3 Replies - 29639 Views - Last Post: 16 October 2008 - 01:17 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=67757&amp;s=41b6839b77262759f26a5ec6a776c3c5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 0
• Posts: 1
• Joined: 15-October 08

# calculate the number of days in a month

Posted 15 October 2008 - 01:15 PM

I cannot figure out what I am doing wrong. I am trying to write a program that prompts the user to enter a month and year and the program calculates the number of days for that month/ year and displays it in the format " August 2007 has 31 days". Any tips to point me in the right direction would be most appreciated.

```import java.util.Scanner;

public class Assignment2
{
public static void main(String[] args)
{
//Create scanner object to obtain input from user
Scanner input = new Scanner (System.in);

int MonthNum; //To hold the month from user input
int Year; //To hold the year
int numDays;
String Month;

MonthNum = input.nextInt();
Year = input.nextInt();

if (MonthNum == 2)
{
if ( (Year % 4 == 0) && (Year % 400 == 0)
&& !(Year % 100 == 0) )
numDays = 29;
else
numDays = 28;
}
else if (MonthNum == 1 || MonthNum == 3 || MonthNum == 5 || MonthNum == 7 || MonthNum == 8
|| MonthNum == 10 || MonthNum == 12)
numDays = 31;
else
numDays = 30;

if (MonthNum == 1)
Month = "January";
else if (MonthNum == 2)
Month = "Feburary";
else if (MonthNum == 3)
Month = "March";
else if (MonthNum == 4)
Month = "April";
else if (MonthNum == 5)
Month = "May";
else if (MonthNum == 6)
Month = "June";
else if (MonthNum == 7)
Month = "July";
else if (MonthNum == 8)
Month = "August";
else if (MonthNum == 9)
Month = "September";
else if (MonthNum == 10)
Month = "October";
else if (MonthNum == 11)
Month = "November";
else if (MonthNum == 12)
Month = "December";

System.out.println(Month + " " + Year " has " + numDays "." );
System.out.println(Month);
System.out.println(numDays);
}
}

```

Is This A Good Question/Topic? 0

## Replies To: calculate the number of days in a month

### #2 DaneAU

• Great::Southern::Land

Reputation: 286
• Posts: 1,619
• Joined: 15-May 08

## Re: calculate the number of days in a month

Posted 15 October 2008 - 02:08 PM

I know this may sound annoying, however there was recently a similar topic posted in java section. Perhaps have a search for it, from memory pbl helped the user significantly and my guess is the answers to your question lies there

ps if i were you for the sake of ease, if the user is to enter a month as an integer... It may be easier to use array's to determine the string interpretation of it... less if else statements

This post has been edited by bbq: 15 October 2008 - 02:09 PM

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: calculate the number of days in a month

Posted 15 October 2008 - 03:39 PM

By the way kind of ugly:

```if (MonthNum == 1)
Month = "January";
else if (MonthNum == 2)
Month = "Feburary";
else if (MonthNum == 3)
Month = "March";
else if (MonthNum == 4)
Month = "April";
else if (MonthNum == 5)
Month = "May";
else if (MonthNum == 6)
Month = "June";
else if (MonthNum == 7)
Month = "July";
else if (MonthNum == 8)
Month = "August";
else if (MonthNum == 9)
Month = "September";
else if (MonthNum == 10)
Month = "October";
else if (MonthNum == 11)
Month = "November";
else if (MonthNum == 12)
Month = "December";

```

```String[] monthName = {"", "January", "February", "March", .....};

Month = monthName[MonthNum];

```

### #4 DaneAU

• Great::Southern::Land

Reputation: 286
• Posts: 1,619
• Joined: 15-May 08

## Re: calculate the number of days in a month

Posted 16 October 2008 - 01:17 AM

Yep thats exactly what i was talking about pbl