0

I'm using a for loop in a method to carry the result to the main function. I'm trying to use the for loop to get the month of a year and pass it on to the output from the main function.

I have nested an if loop in the for loop which I feel is probably redundant as the for loop will count to the end anyway. It's probably a basic enough problem in the code but I have been staring at it for so long that I think it's burn out on my end.

The output is returning "Doesn't Exist" for all months instead of picking out the relevant month. How do I pick out the relevant month from the for loop or is that possible with the way I have coded so far?

namespace Month_Function_Call
    {
class Program
{
    public static String month_name(int month)
    {
        String result;
        result = "a";
        for (int i = 0; i < 12; ++i )
        {
            if (i == 0)
            {
                result = "January";
            }
            if (i == 1)
            {
                result = "February";
            }
            if (i == 2)
            {
                result = "March";
            }
            if (i == 3)
            {
                result = "April";
            }
            if (i == 4)
            {
                result = "May";
            }
            if (i == 5)
            {
                result = "June";
            }
            if (i == 6)
            {
                result = "July";
            }
            if (i == 7)
            {
                result = "August";
            }
            if (i == 8)
            {
                result = "September";
            }
            if (i == 9)
            {
                result = "October";
            }
            if (i == 10)
            {
                result = "November";
            }
            if (i == 11)
            {
                result = "December";
            }
            else
            {
                result = "N/A";
            }


        }
            return result;
    }
    static void Main(string[] args)
    {
        Console.WriteLine("Month 1: " + month_name(1));
        Console.WriteLine("Month 2: " + month_name(2));
        Console.WriteLine("Month 3: " + month_name(3));
        Console.WriteLine("Month 4: " + month_name(4));
        Console.WriteLine("Month 5: " + month_name(5));
        Console.WriteLine("Month 6: " + month_name(6));
        Console.WriteLine("Month 7: " + month_name(7));
        Console.WriteLine("Month 8: " + month_name(8));
        Console.WriteLine("Month 9: " + month_name(9));
        Console.WriteLine("Month 10: " + month_name(10));
        Console.WriteLine("Month 11: " + month_name(11));
        Console.WriteLine("Month 12: " + month_name(12));
        Console.WriteLine("Month 43: " + month_name(43));
        Console.ReadKey();
    }
}
9
  • 4
    Why are you looping instead of just comparing month in the if statements? Commented Nov 23, 2015 at 15:11
  • 1
    What problem are you encountering? What help are you actually seeking? If its code review you are after then codereview.stackexchange.com exists and might be a better place for the question (but only if your code actually works). Commented Nov 23, 2015 at 15:11
  • 2
    You don't need a loop in your method, you need the loop in Main to pass month 1 to 12. Also look into Dictionary<TKey,TValue> so that you can get ride of the if statement in your method. Plus there are already methods available to get Month name based on number. One last thing is you need if ... else ... if to sort out invalid choice, you may use switch here as well. Commented Nov 23, 2015 at 15:12
  • 2
    stackoverflow.com/questions/3184121/… Commented Nov 23, 2015 at 15:15
  • 3
    @Damo, your main problem is you have a method accepting an int representing the month, but you never reference that object. You method should use the information from its parameters in calculating a result. Commented Nov 23, 2015 at 15:15

6 Answers 6

7

I think using GetMonthName from DateTimeFormat would be a better way of doing it. This will give you the name in the users active culture. (You can of course hard code this to any culture you like) Then ToTitleCase to get the first character as upper case.

public static String month_name(int month)
{
   if(month < 1 || month > 12) 
      return "N/A";
   var culture = CultureInfo.CurrentCulture;
   var name = culture.DateTimeFormat.GetMonthName(month);
   return culture.TextInfo.ToTitleCase(name);
}
Sign up to request clarification or add additional context in comments.

4 Comments

First the OP is using a zero based index for the month names and this uses a 1 based. Second this returns an empty string for a value of 13 and throws an exception for values less than 1 and greater than 13.
@juharr As for zero based index that seems to be wrong in his function if you look at his Main method it assumes 1 to be the first month.
@juharr you're right, that was a mistake I spotted after you mentioned it, took me a while to figure out why I got N\A for two months instead of one but it was the index that was wrong
@Magnus Yeah I just noticed that too and asked the OP to clarify
5

You could make this cleaner by using switch

 switch (month)
        {
            case 0: return "January";
            case 1: return "February";
            case 2: return "March";
            case 3: return "April";
            case 4: return "May";
            case 5: return "June";
            case 6: return "July";
            case 7: return "August";
            case 8: return "September";
            case 9: return "October";
            case 10: return "November";
            case 11: return "December";
            default: return "N/A";
        }

Comments

4

Do something like this instead

string[] months = new string[12] {"January", "February", "March" }; // Input all months to this array

if (index <= -1 || index > 12) return "N/A";
return months[index];

Insert this code into the getMonthName() function

4 Comments

I saw your 43 test case, let me edit the answer that suits that, hmm.
Thank you that makes infinitely more sense than the nonsense I was typing! I knew it would be something simple that I missed
Just do return index >= 0 && index < 12 ? months[index] : "N/A";
@TuukkaX Yeah, I actually added that comment before you added the if though you current have the logic backwards.
3

Dont use loop and if-else. What you need is dictionary.

static Dictionary<int, string> _monthName = new Dictionary<int, string>
{
    {1,"January" }, // if zero based start from 0
    {2,"February" },
    {3,"March" },
    {4,"April" },
    {5,"May" },
    {6,"June" },
    {7,"July" },
    {8,"August" },
    {9,"September" },
    {10,"October" },
    {11,"November" },
    {12,"December" },
};

private static string GetMonthName(int i)
{
    var result = "";
    if (_monthName.TryGetValue(i, out result))
    {
        return result;
    }
    return "N/A";
}
static void Main(string[] args)
{
    Console.WriteLine("Month 1: " + GetMonthName(1));
    Console.WriteLine("Month 2: " + GetMonthName(2));
    Console.WriteLine("Month 3: " + GetMonthName(3));
    Console.WriteLine("Month 4: " + GetMonthName(4));
    Console.WriteLine("Month 5: " + GetMonthName(5));
    Console.WriteLine("Month 6: " + GetMonthName(6));
    Console.WriteLine("Month 7: " + GetMonthName(7));
    Console.WriteLine("Month 8: " + GetMonthName(8));
    Console.WriteLine("Month 9: " + GetMonthName(9));
    Console.WriteLine("Month 10: " + GetMonthName(10));
    Console.WriteLine("Month 11: " + GetMonthName(11));
    Console.WriteLine("Month 12: " + GetMonthName(12));
    Console.WriteLine("Month 43: " + GetMonthName(43));
    Console.ReadKey();
}

Comments

1

In my opinion You should ommit using so many if statements in the for loop in this case because it is not readable enough and the better approach would be to create an array of type string which will have all the names of the months and to iterate this array. Your code would be like:

public static String month_name(int month) {
        String result;
        result = "a";
        // for the sake of readability I have split the line
        String[] allMonths = { 
                              "N/A", "January", "February", "March", "April",
                              "May", "June", "July", "August", "September", 
                              "October", "November", "December" 
                          };
        if (month >= 0 && month <= 12)
            result = allMonths[month];
        else
           result = "N/A";

        return result;
    }

    static void Main(string[] args) {
        Console.WriteLine("Month 1: " + month_name(1));
        Console.WriteLine("Month 2: " + month_name(2));
        Console.WriteLine("Month 3: " + month_name(3));
        Console.WriteLine("Month 4: " + month_name(4));
        Console.WriteLine("Month 5: " + month_name(5));
        Console.WriteLine("Month 6: " + month_name(6));
        Console.WriteLine("Month 7: " + month_name(7));
        Console.WriteLine("Month 8: " + month_name(8));
        Console.WriteLine("Month 9: " + month_name(9));
        Console.WriteLine("Month 10: " + month_name(10));
        Console.WriteLine("Month 11: " + month_name(11));
        Console.WriteLine("Month 12: " + month_name(12));
        Console.WriteLine("Month 43: " + month_name(43));
        Console.ReadKey();
    }

Hope it helped :)

Comments

0

Using the Swith-Case statement is the best choice here.

But, if you don't know how to use it, you should remove the For statement, because it is completely non-sense.

 //for (int i = 0; i < 12; ++i )
    //{

    //}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.