Thread: I need am converting an " if else statement" to a "switch statement"

1. Member
Join Date
Jan 2014
Posts
1
Rep Power
0

I need am converting an " if else statement" to a "switch statement"

I clueless on how to do this

my code is

import java.util.Scanner;
public class mylab

{
public static void main(String[] args)

{ Scanner in = new Scanner(System.in);

int month;
int day;
String season= "seasons";

System.out.print("type a two digit month");
System.out.print(" and day");
month = in.nextInt();
day = in.nextInt();

String fall = " fall";
String winter = " winter ";
String summer = " summer";
String spring = " spring";

System.out.print(" Month="+ month +" Day= "+day);

if( month <= 3)
{ System.out.println(" Winter");
season= winter; }
else if ( month <=6)
{ System.out.println(" Spring ");
season=spring; }

else if ( month<= 9)
{ System.out.println(" Summer ");
season= spring; }

else if ( month<=12)
{ System.out.println(" Fall");
season= fall; }

I just need to change the bolded part to a switch

if (month % 3 == 0 && day >= 21)
{
if ( season.equals(winter) )
System.out.println(" Spring");

else if ( season.equals(spring) )
System.out.println ( "Summer" );

else if ( season.equals(summer) )
System.out.println ( " Fall");

else if ( season.equals(winter) )
System.out.println( " winter");

}

}
}

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,661
Rep Power
9

Re: I need am converting an " if else statement" to a "switch statement"

First, the vernal equinox occurs around the 20th of March so your day is wrong in your if statement. Switch statements do not readily lend themselves to tests that involve ranges. The easiest way is to enumerate all the months.

Regards,
Jim
Last edited by jim829; 01-30-2014 at 05:32 AM.

3. Señor Member
Join Date
Jan 2014
Posts
184
Rep Power
0

Re: I need am converting an " if else statement" to a "switch statement"

Here is a link to some good switch statement info and examples
The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

The issue that Jim is talking about is that switch statements work by cases

For example, here is a sample switch statement
Java Code:
```int a = 12;
switch (a) {
case 4:
System.out.println("The number is 4!");
break;

case 7:
System.out.println("The number is 7!");
break;

case 12:
System.out.println("The number is 12!");
break;

default:
System.out.println("I don't know what the number is...");
}```
This should print out "The number is 12!".

Notice 2 things - first of all, notice that in a switch statement, brackets only go around the OUTSIDE. Individual cases don't have brackets.

Second, there needs to be a break in each case - this ends the checking for the switch.

Also, notice that you can only check for a single case.
In other words, you can NOT do
Java Code:
`case 5-8: dothis; break;   //you can't do this`
I'm assuming that you need to make it into a switch for a homework assignment.

A shortcut, so that you don't have to have a case for each number, is this:

If a set of numbers trigger a single result, you can do this:
Java Code:
```case 1:
case 2:
case 3:
dothis;
break;```

Java Code:
```case 1:
dothis;
break;

case 2:
dothis;
break;

case 3:
dothis;
break;```
They both do the same thing

If I remember correctly, the switch statement will check from top to bottom. Once a case is found, it will "break" afterwards and not keep looking. Think about this to figure out the order that you'll want to put your cases in.
Last edited by AlexGraal; 01-30-2014 at 05:52 AM.

4. Re: I need am converting an " if else statement" to a "switch statement"

Originally Posted by AlexGraal
In other words, you can NOT do
Java Code:
`case 5-8: dothis; break;   //you can't do this`
Well, as a matter of fact, you can do that, but it probably won't do what you wanted, i.e. it switches on value -3 ...

kind regards,

Jos