I am working on a project where you enter the abbreviation for a state into a scanner and then the program tells you what region of the U.S. the state is in. I think that the majority of my code is correct, but I have never used switch before.
I have polished my work down to one error, a missing return statement in my first block of code (the switch section). I do not know if any other code is affecting it, so I am just going to post most of it here. Thanks in advance.
import java.util.Scanner;
public class Regions {
public static String getRegion (String stateName){
String region = "";
switch(stateName){
case "ME":
case "VT":
case "NH":
case "MA":
case "CT":
case "RI":
region = "New England";
break;
case "NY":
case "NJ":
case "DE":
case "MD":
case "VA":
case "NC":
case "SC":
region = "Atlantic";
break;
case "GA":
case "FL":
case "MS":
case "AL":
case "LA":
case "TN":
region = "Southeast";
break;
case "PA":
case "OH":
case "MI":
case "IN":
case "IL":
case "WI":
case "MN":
case "KY":
case "WV":
case "IA":
region = "Midwest";
break;
case "ND":
case "SD":
case "KS":
case "NE":
case "MO":
region = "Great Plains";
break;
}
}
public static void main (String[]args){
Scanner s = new Scanner(System.in);
System.out.println("Enter a two-letter state abbreviation:");
String stateName = s.nextLine();
String region = getRegion(stateName);
System.out.print("The state " + stateName);
if (region == "New England"){
System.out.print(" is in the New England region!");
}
else if (region == "Atlantic") {
System.out.print(" is in the Atlantic region!");
}
else if (region == "Southeast") {
System.out.print(" is in the Southeast region!");
}
else if (region == "Midwest") {
System.out.print(" is in the Midwest region!");
}
else if (region == "Great Plains") {
System.out.print(" is in the Great Plains region!");
}
else {
System.out.println ("That's not a state.");
}
}
}`