4 Replies - 1701 Views - Last Post: 18 March 2009 - 09:01 AM Rate Topic: -----

#1 Russo08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-March 09

missing return statement

Posted 17 March 2009 - 01:06 PM

im trying to make this switch statement but i keep getting this missing return statement when i have have any ideas on what could be wrong? Much appreciated thanks.

well here is my entire code but i just need help with the switch statement.

 import java.util.Scanner;

public class differentDates
{
	public static void main (String [] args)
		{
			int y1,y2,m1,m2,d1,d2;
			long tdays = 0;
			
			Scanner scan = new Scanner(System.in);
			System.out.println ("Enter the first date in the format m d yyyy");
			m1 = scan.nextInt();
			d1 = scan.nextInt();
			y1 = scan.nextInt();
			
			System.out.println (" Enter the second date in the format m d yyyy");
			m2 = scan.nextInt ();
			d2 = scan.nextInt ();
			y2 = scan.nextInt ();
			
			if (m1 == m2 && y1 == y2)
			{
				tdays = d2-d1;
			}
			else if ( y1 == y2) 
			{
				for ( int i = m1 + 1; i < m2; ++i)
				tdays += daysInMonth ( i,  y1);
				tdays += d2;
				tdays +=daysInMonth ( m1, y1)- d1 + 1;
			}
			
			System.out.println ("The Total Number Of Days is: " + tdays);
		}
	public static int daysInMonth ( int m ,int y)
		{ 
			switch (m)
				{
					case 1:
					case 3:
					case 5:
					case 7:
					case 8:
					case 10: 
					case 12: return 31;
					case 4:
					case 6:
					case 9:
					case 11: return 30;
					case 2: if (isLeap(y) )
						{
							return 29;
						}
						else
						{
							return 28;
						}
				}
		}
	public static boolean isLeap (int y)
		{
			if ( y % 4 == 0)
			{
				return true;
			}
			else if (y % 4 == 0 && y % 100 != 0)
			{
				return true;
			}
			else 
			{
				return false;
			}
		}
	public static int daysInYear (int y)
		{
			if (true)
			{ 
				return 366;
			}
			else 
			{ 
				return 365;
			}
		}
}	 


Is This A Good Question/Topic? 0
  • +

Replies To: missing return statement

#2 mostyfriedman  Icon User is offline

  • The Algorithmi
  • member icon

Reputation: 727
  • View blog
  • Posts: 4,473
  • Joined: 24-October 08

Re: missing return statement

Posted 17 March 2009 - 01:22 PM

in your days in a month method, what if the user inserts a number that does not have a corresponding return statement then what will it return??
Was This Post Helpful? 0
  • +
  • -

#3 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: missing return statement

Posted 17 March 2009 - 01:41 PM

In other words, you have to return something in any case.
Was This Post Helpful? 0
  • +
  • -

#4 Russo08  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-March 09

Re: missing return statement

Posted 18 March 2009 - 07:53 AM

View PostLocke, on 17 Mar, 2009 - 12:41 PM, said:

In other words, you have to return something in any case.


Yea im trying to get it to return a "date invalid" using the default case but it still gives me the same error.
Was This Post Helpful? 0
  • +
  • -

#5 Alexandra N  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 18-March 09

Re: missing return statement

Posted 18 March 2009 - 09:01 AM

public static int daysInMonth ( int m ,int y)
		{
			int x = 0;
			switch (m)
				{
					case 1:
					case 3:
					case 5:
					case 7:
					case 8:
					case 10:
					case 12: x = 31;
					case 4:
					case 6:
					case 9:
					case 11: x = 30;
					case 2: if (isLeap(y) )
						{
							x = 29;
						}
						else
						{
						   x = 28;
						}
				}
return x;
		}



I just gave you an example, but you have to decide what happens when m is none of the values you wrote in your switch.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1