5 Replies - 197 Views - Last Post: 06 October 2012 - 02:06 PM Rate Topic: -----

#1 aejames23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-September 12

Else if to Case

Posted 06 October 2012 - 12:37 PM

Im just having trouble changing an if, else statement into a switch/case.
if (1582 > year)		
			System.out.println("Invalid Year");		
		else if ((month > 12) || (month < 1))		
			System.out.println("Invalid Month");
		
		if ((month == 1) && (day > 31))
			System.out.println("Invalid Day");
		else if ((month == 2) && (day > 28))
			System.out.println("Invalid Day");
		else if ((month == 3) && (day > 31))
			System.out.println("Invalid Day");
		else if ((month == 4) && (day > 30))
			System.out.println("Invalid Day");
		else if ((month == 5) && (day > 31))
			System.out.println("Invalid Day");
		else if ((month == 6) && (day > 30))
			System.out.println("Invalid Day");
		else if ((month == 7) && (day > 31))
			System.out.println("Invalid Day");
		else if ((month == 8) && (day > 31))
			System.out.println("Invalid Day");
		else if ((month == 9) && (day > 30))
			System.out.println("Invalid Day");
		else if ((month == 10) && (day > 31))
			System.out.println("Invalid Day");
		else if ((month == 11) && (day > 30))
			System.out.println("Invalid Day");		
		else if ((month == 12) && (day > 31))
			System.out.println("Invalid Day");



How would i use switch here?

Is This A Good Question/Topic? 0
  • +

Replies To: Else if to Case

#2 NathanMullenax  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 83
  • View blog
  • Posts: 176
  • Joined: 23-September 12

Re: Else if to Case

Posted 06 October 2012 - 01:01 PM

You would probably switch on the month and set a noOfDays variable, and then check if the day is valid after the switch statement.

Like,
switch(month)
{
    case 1: 
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        noOfDays = 31;
    break;
    // and so on. 
}

if( day > noOfDays )
{
    // error message
}


If the switch statement is exhaustive (that is, has a rule for 1..12) then the 'default' label should probably report an 'invalid month' error.

Hope this helps.
Was This Post Helpful? 1
  • +
  • -

#3 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: Else if to Case

Posted 06 October 2012 - 01:11 PM

In order to switch an if else to a switch statement you would do this
int var1 =;
switch (var1) {
     case 1:
	// add statement here
	break;
     case 2:
	// add statement here
	break;
     case 3:
	// add statement here
	break;
     default:  // optional default case
			   
     } // end of switch statement
/* 
 * note that a switch statement can take an int, short, byte, or char variable 
 * if you have java 7 it can also take a string as well.
 */



All you have to do is fill in the cases with your print statements. What ever var1 is, is the case it will use.

This post has been edited by Ytry: 06 October 2012 - 01:15 PM

Was This Post Helpful? 0
  • +
  • -

#4 aejames23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 28-September 12

Re: Else if to Case

Posted 06 October 2012 - 01:43 PM

Thanks for the help guys however, i am really having trouble turning something like this into cases...
	if ((year % 4 == 0))
			{			
			
			if ((year % 100 != 0)) 
			{
				System.out.println(year + " is");
				
			}		   
			else if (year % 400 == 0) 
			{				
			System.out.println(year + " is ");
			
			}
		    else 
		    {
			System.out.println(year + " is not");
			
		    }
			
						
		}
		
		else
			{
			System.out.println(year + " is not.");
								
			}
			

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10193
  • View blog
  • Posts: 37,641
  • Joined: 27-December 08

Re: Else if to Case

Posted 06 October 2012 - 01:48 PM

You haven't even made an effort to implement a switch statement.
Was This Post Helpful? 0
  • +
  • -

#6 Ytry  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 120
  • Joined: 25-July 12

Re: Else if to Case

Posted 06 October 2012 - 02:06 PM

Okay, I am not going to do this for you because if I did what would you learn? I will give you this though maybe it will help.
int age;
//this is an if statement that I want to turn into a switch statement
if(age == 2){
System.out.println("You are two years old");
} else if(age == 1){
System.out.println("You are one year old");
} else if(age == 3){
System.out.println("You are 3 years old");
}



If I were to continue with this if statement it would be very tedious to check the age every single time then tell it what to do, so I will use a switch statement instead.

int age;
switch(age){
case 1:
System.out.println("You are one year old");
break;
case 2:
System.out.println("You are two years old");
break;
case 3:
System.out.println("You are three years old");
break;
default: System.out.println("I don't know how old you are");
}



This is much easier as I don't have to write the test condition over, and over again.

This post has been edited by Ytry: 06 October 2012 - 02:06 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1