0

hello i am supposed to design a code to convert state abbreviations from only five states. I don't really know how to put if...else statement in here if anyone can help. This is what I have written so far but i am beginning so it could be completely wrong.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace week0701
{
   class Program
   {
      static void Main(string[] args)
      {
         string option;
         string NC;
         string AL;
         string GA;
         string FL;
         string OH;

         Console.WriteLine("Please enter one of the following state abbreviations");
         Console.WriteLine("NC, AL, GA, FL, or OH: ");
         Console.ReadLine();

         switch (option.ToLower())
         { // create switches for lower cases 
            case "nc":
               toNC();
               break;
            case "al":
               toAL();
               break;
            case "ga":
               toGA;
               break;
            case "fl":
               toFL;
               break;
            case "oh":
               toOH;
               break;
         }
      }
      static void toNC()
      {
         // assign variables
         string state = "North Carolina";
         Console.WriteLine("NC is {0}", state);
      }
      static void toAL()
      {
         // al to alabama
         string state = "Alabama";
         Console.WriteLine("AL is {0}", state);
      }
      static void toGA()
      {
         // ga to georgia
         string state = "Georgia";
         Console.WriteLine("GA is {0}", state);
      }
      static void toFL()
      {
         // fl to florida
         string state = "Florida";
         Console.WriteLine("FL is {0}", state);
      }
      static void toOH()
      {
         // oh to ohio
         string state = "Ohio";
         Console.WriteLine("OH is {0}", state);
      }
   }
}
4
Contributors
7
Replies
8
Views
7 Years
Discussion Span
Last Post by rzhaley
1

Hey rzhaley

You have many methods there and you could put that into one method. If else would be ok in this case, but I prefer a swith statement in this case as you have done.

How about a method like this:

public string FromAbbreviation(string abr)
        {
            switch (abr.ToLower().Trim())
            {
                case "nc":
                    return "North Carolina";
                case "al":
                    return "Alabama";
                case "ga":
                    return "Georgia";
                case "fl":
                    return "Florida";
                case "oh":
                    return "Ohio";
            }
            return "Abbreviation unrecognised";
        }

That will take the abbreviation and return the full name.

Hope that helps

Edited by Tower-Rounder: n/a

Votes + Comments
Good explanation
0

thanks that made it a bit clearer, i changed it to this but now it's not returning the full state name

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace week0701
{
   class State
   {
      static void Main(string[] args)
      {
         string abr;

         Console.WriteLine("Please enter one of the following state abbreviations");
         Console.WriteLine("NC, AL, GA, FL, or OH: ");
         abr = Console.ReadLine();

         Console.WriteLine();
         Console.WriteLine("State is {0}", abr);
         Console.ReadLine();
      }
      public string FromAbbreviation(string abr)
         {
         switch (abr.ToLower().Trim())
         { // create switches for lower cases 
            case "nc":
               return "North Carolina";
            case "NC":
               return "North Carolina";
            case "al":
               return "Alabama";
            case "AL":
               return "Alabama";
            case "ga":
               return "Georgia";
            case "GA":
               return "Georgia";
            case "fl":
               return "Florida";
            case "FL":
               return "Florida";
            case "oh":
               return "Ohio";
            case "OH":
               return "Ohio";
         }
         return "Wrong Abbreviation";
      }
   }
}
0

You didn't call your method. At line 19 you are writing out the abbreviation. Try:

Console.WriteLine("State is {0}", FromAbbreviation(abr));

In addition, you are already comparing to lower text switch(abr.ToLower().Trim()) at line 24, so all of those case labels you have for checking upper-case abbreviations will never test true--you can remove them.

Edited by DdoubleD: n/a

0

oh ok i understand the upper lower case now oops. i tried calling the method but it gives me an error so thats why i had that deleted. it says
Error 1 An object reference is required for the non-static field, method, or property 'State.FromAbbreviation(string)'

0

Hey.

That is because the FromAbbreviation(...) method also needs to be marked as static. Should work fine after that.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.