3 Replies - 24968 Views - Last Post: 16 October 2008 - 01:17 AM Rate Topic: -----

#1 nadroj  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;
		
		System.out.print("Please enter the Month #");
		MonthNum = input.nextInt();
		System.out.print("Please enter the Year");
		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  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8346
  • View blog
  • Posts: 31,910
  • 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];


Was This Post Helpful? 0
  • +
  • -

#4 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 286
  • View blog
  • 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 :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1