0

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.");
       }
     }

  }`
Md873
  • 1

3 Answers3

1

You eventually need to add a result when to the method is passed an invalid state.

The rest of the code is correct, but you need to return region at the end of the method.

A better code is to return directly instead of using a locale variable region.

  public static String getRegion (String stateName){

    switch (stateName) {
      case "ME":
      case "VT":
      case "NH":
      case "MA":
      case "CT":
      case "RI":
         return "New England";

      case "NY":
      case "NJ":
      case "DE":
      case "MD":
      case "VA":
      case "NC":
      case "SC":
         return "Atlantic";

      case "GA":
      case "FL":
      case "MS":
      case "AL":
      case "LA":
      case "TN":
         return "Southeast";

      case "PA":
      case "OH":
      case "MI":
      case "IN":
      case "IL":
      case "WI":
      case "MN":
      case "KY":
      case "WV":
      case "IA":
         return "Midwest";

      case "ND":
      case "SD":
      case "KS":
      case "NE":
      case "MO":
         return "Great Plains";


    }
    throw new IllegalArgumentException("invalid state");
    // Or return a special string value
  }

As other said there are errors also in the main, but I focused only in the getRegion method that was the question asked. Don't use == operator to compare strings, use the method equals.

== check if two strings are the same object, equals check if two string objects have the same content.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
1

First Problem : Missing return in getRegion()
Just add

return region;

add the end of that method.

Second Problem : Comparing strings with == instead of equals
In your main replace all

if (region == "..."){

with

if (region.equals("...")){
Turamarth
  • 2,282
  • 4
  • 25
  • 31
0

Looks like you just want to put a

return region;

at the end of your getRegion method. That's it.

Zyndoras
  • 33
  • 1
  • 10