0

Basic Info

  • Programming Language: C# (C Sharp)

  • Program Type: Console .Net Core 2.0 Application

  • IDE: Visual Studios Community 2017 v15.8.7

Program Info

Program Name: Calendar Counter

Basics of my Program: User types in a month and it tells them how many days are in that month. The user is free to type any known combination of month name/identity (e.g. March, march, mar, Mar, Mar., mar., 03, or 3).

My Programs Execution: The program asks the user for a month and they type it in. The program then checks what the user typed to a method named MonthDatabase() with a String Array. If it matches it will display the month they typed in with how many days it has.

Issue Info

Program Issue: My main issue is finding a way to check what the user inputted with what's in the String Array. I've tried switches and different if statements, but most give me an error (I won't be posting all of those). I also tried it using the dictionary and a list. Each produced their own errors. So I went back to using a string array and after many searches and trying different things that just didn't work. I even tried to google for what I was looking to accomplish (e.g. check user input with string array) to no avail. Currently, the error I get is:

Delegate to an instance method cannot have null 'this'.

Now being new to C# (and programming in general) I could only assume that it wants me to specify what to check in the string array to match it to the user input. I tried something like:

Console.Write("Enter a month?: ");
string userMonth = (Console.ReadLine());

if (userMonth == mData(1, 1)) //Compare user input to MonthDataBase
        {
            Console.WriteLine("You typed a word found in the datatbase.");
        }
        else
        {
            Console.WriteLine("You didn't type a word in the datatbase.");
        }

AND

Console.Write("Enter a month?: ");
string userMonth = (Console.ReadLine());

if (userMonth == mData[1, 1]) //Compare user input to MonthDataBase
        {
            Console.WriteLine("You typed a word found in the datatbase.");
        }
        else
        {
            Console.WriteLine("You didn't type a word in the datatbase.");
        }

Thinking it would look at the string in that position and match it, but clearly, I am wrong on that assumption. I even tried to Google to see if there was a way to just simply match what the user typed to the whole string array, but found nothing.

I have spent a week trying to figure this out and decided to come here to see if anyone can point me in the right direction. I like the idea of learning it so if possible no direct answers as "do this" and it will work. Maybe an example (different, but close to what I'm trying to do) or "read this" and it should help you.

The Code

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

namespace Calendar_Counter
{
    class Program
    {
    public static string mData { get; private set; } //Use mData in any method.

    static void Main(string[] args)
        {
            Header(); //Call Header Method & display
            Menu(); //Call Menu Method & display
            CCDatatbase(); //Call Calendar Counter Database Method, execute & display.

            //Console.WriteLine("Hello World!");
            ExitProgram(); //Call exit program, execute & display
        }
        static void Header()
        {
            Console.Clear(); //Clear console buffer & console window of display information
            Console.Write("--------------------\n| Calendar Counter |\n--------------------\n"); //Display Header text
        }
        static void Menu()
        {
            //ADD menu options once basic program is working!!
            Console.WriteLine(); //Space
            Console.Write("MENU: //ADD menu options once basic program is working!!");
            Console.WriteLine("\n"); //Double Space
        }
        static void CCDatatbase()
        {

            Console.Write("Enter a month?: ");
            string userMonth = (Console.ReadLine());

            if (userMonth.Any(mData.Contains)) //Compare user input to MonthDataBase
            {
                Console.WriteLine("You typed a word found in the datatbase.");
            }
            else
            {
                Console.WriteLine("You didn't type a word in the datatbase.");
            }
        }
        public static string[,] MonthDataBase() //Month Database
        {
            //Check user input with Array List.
            string[,] mData = new string[12, 8]
            {
                { "January", "january", "Jan", "jan", "Jan.", "jan.", "1", "01" }, //If user types 1-8 display corisponding message in CCDatatbase()
                { "January", "january", "Feb", "feb", "Feb.", "feb.", "2", "02" },
                { "March", "march", "Mar", "mar", "Mar.", "mar.", "3", "03" },
                { "April", "april", "Apr", "apr", "Apr.", "apr.", "4", "04" },
                { "May", "may", "May", "may", "May", "may", "5", "05" },
                { "June", "june", "Jun", "jun", "Jun.", "jun.", "6", "06" },
                { "July", "july", "Jul", "jul", "Jul.", "jul.", "7", "07" },
                { "August", "august", "Aug", "aug", "Aug.", "aug.", "8", "08" },
                { "September", "september", "Sep", "sep", "Sep.", "sep.", "9", "09" },
                { "October", "october", "Oct", "oct", "Oct.", "oct.", "10", "10" },
                { "November", "november", "Nov", "nov", "Nov.", "nov.", "11", "11" },
                { "December", "december", "Dec", "dec", "Dec.", "dec.", "12", "12" }
            };

            return mData;
        }
        static void ExitProgram()
        {
            //REPLACE later with an actual exit option in menu!!
            Console.Write("EXIT: //REPLACE later with an actual exit option in menu!!\n\n");
            //Prevent Debugging test from closing.
            Console.Write("Press any key to Exit...");
            Console.ReadLine();
        }
    }
}
1
  • Looks like this statement "if (userMonth.Any(mData.Contains))" - The mData is a property and not the variable which is declared in MonthDatabase(). And I can't see from where you were calling MonthDatabase() in your whole program? Commented Oct 20, 2018 at 7:14

2 Answers 2

1

If you don't have any intention to make it a double-dimensional array, then I would suggest go with creating a class. A class will simplifies many things in a long run rather than depending on a double-dimensional array in this case.

Here is my approach for the problem.

  1. I have created a class called Month (Name you can change it as per your needs).

  2. The class "Month" has a List property MonthNames as shown below,

      public class Month {
      public List<string> MonthNames { get; set; }
    
      public Month()
      {
          MonthNames = new List<string>();
      } }
    
  3. Create the object of Month type as below in your GetMonths() method. (In this case you can discard MonthDataBase() method).

          private static List<Month> GetMonths()
          {
              var months = new List<Month>();
              var month = new Month();
              month.MonthNames.Add("January");
              month.MonthNames.Add("january");
              month.MonthNames.Add("Jan");
              month.MonthNames.Add("01");
              months.Add(month);
    
              month = new Month();
              month.MonthNames.Add("Feburary");
              month.MonthNames.Add("feburary");
              month.MonthNames.Add("Feb");
              month.MonthNames.Add("02");
              months.Add(month);
              // Similary add all other to the list.
    
              return months;
          }
    
  4. Here is the rest of the code (You add this below code where ever you want in your program),

              Console.Write("Enter a month?: ");
              var userMonth = (Console.ReadLine());
              var months = GetMonths();
    
              var result = months.Where(x => x.MonthNames.Any(y => y.Equals(userMonth))).ToList();
    

Note:- I have returned as ToList(), but in your case you can return as FirstOrDefault(). So now you have a Month object with you and based on the data, you can print the no. of days or do some other thing is upto you.

Hope it is clear for you. Give a try and lets know in case of any errors or doubts.

Sign up to request clarification or add additional context in comments.

5 Comments

It throws an error for the GetMonths(): "Does not exist in the current context. This is the same error I got when I tried to do it as a class. I tried: GetMonths = MonthDatabase(), but that throws its own errors. Visual Studios suggests: private static object GetMonths() { throw new NotImplementedException(); } I also tried adding: class Program : MonthDatabase, but that didn't help.
Alright so according to this topic stackoverflow.com/questions/52903218/… my issue would be: private static List<Month> GetMonths() when I change it to public static List<MonthDatabase> GetMonths() the error goes away. Being new I'm going to assume you set it to private for a reason and if so why and how to make it work.
Few points to note down, When it throws GetMonths() does not exists, I assume that is because of "private" access modifiers. And I am not sure where you have created the "GetMonths()" method. In the above code which I pasted, I have added in "Program" class and hence it make sense for me.
This statement in your comment "class Program : MonthDatabase" - MonthDatabase is a method and you are trying to inherit a class from a method? That is not correct. A class can inherit another class or an interface. I guess you might need to study OO principles first.
Sorry for the delayed response, as per your second comment, hope it works for you. In case if any issues, do let us know. If it satisfies your requirements then you mark my answer as accepted.
0
  1. Try to change return type of your MonthDataBase method to List<List<string>>.

  2. Try to change the signature of mData public propery to public static List<List<string>> mData { get; private set; }

  3. Fill above property mData in CCDatatbase method like mData = MonthDataBase();

  4. Then check whether entered string is present in mData or not.

So finally your program will be.

class Program
{
    public static List<List<string>> mData { get; private set; } //Use mData in any method.

    static void Main(string[] args)
    {
        Header();       // Call Header Method & display
        Menu();         // Call Menu Method & display
        CCDatabase();   // Call Calendar Counter Database Method, execute & display.

        //Console.WriteLine("Hello World!");
        ExitProgram(); //Call exit program, execute & display
    }

    static void Header()
    {
        Console.Clear(); //Clear console buffer & console window of display information
        Console.Write("--------------------\n| Calendar Counter |\n--------------------\n"); //Display Header text
    }

    static void Menu()
    {
        //ADD menu options once basic program is working!!
        Console.WriteLine(); //Space
        Console.Write("MENU: //ADD menu options once basic program is working!!");
        Console.WriteLine("\n"); //Double Space
    }

    static void CCDatabase()
    {
        mData = MonthDataBase();
        Console.Write("Enter a month?: ");
        string userMonth = (Console.ReadLine());

        if (mData.Any(x => x.Contains(userMonth))) //Compare user input to MonthDataBase
        {
            var month = mData.Where(x => x.Contains(userMonth)).Select(x => new { Days = x[0], Name = x[1] }).FirstOrDefault();
            Console.WriteLine($"{month.Name} has {month.Days} days in it.");
            Console.WriteLine();
        }
        else
        {
            Console.WriteLine("You didn't type a word in the database.");
            Console.WriteLine();
        }
    }

    public static List<List<string>> MonthDataBase() //Month Database
    {
        var mData = new List<List<string>>  {
      new List<string>  { "31", "January", "january", "Jan", "jan", "Jan.", "jan.", "1", "01" }, //If user types 1-8 display corresponding message in CCDatatbase()
      new List<string>  { "28/29", "February", "february", "Feb", "feb", "Feb.", "feb.", "2", "02" },
      new List<string>  { "31", "March", "march", "Mar", "mar", "Mar.", "mar.", "3", "03" },
      new List<string>  { "30", "April", "april", "Apr", "apr", "Apr.", "apr.", "4", "04" },
      new List<string>  { "31", "May", "may", "May", "may", "May", "may", "5", "05" },
      new List<string>  { "30", "June", "june", "Jun", "jun", "Jun.", "jun.", "6", "06" },
      new List<string>  { "31", "July", "july", "Jul", "jul", "Jul.", "jul.", "7", "07" },
      new List<string>  { "31", "August", "august", "Aug", "aug", "Aug.", "aug.", "8", "08" },
      new List<string>  { "30", "September", "september", "Sep", "sep", "Sep.", "sep.", "9", "09" },
      new List<string>  { "31", "October", "october", "Oct", "oct", "Oct.", "oct.", "10", "10" },
      new List<string>  { "30", "November", "november", "Nov", "nov", "Nov.", "nov.", "11", "11" },
      new List<string>  { "31", "December", "december", "Dec", "dec", "Dec.", "dec.", "12", "12" }
    };

        return mData;
    }

    static void ExitProgram()
    {
        //REPLACE later with an actual exit option in menu!!
        Console.Write("EXIT: //REPLACE later with an actual exit option in menu!!\n\n");
        //Prevent Debugging test from closing.
        Console.Write("Press any key to Exit...");
        Console.ReadLine();
    }
}

Input: jul

Output:

enter image description here

Input: 04

Output:

enter image description here

Input: abcd

Output:

enter image description here

6 Comments

Yes, it works. Now I just need to figure out how to check what the user typed with one row (January) and have it display a different message for each row (month).
@DragonflyCode, i didn't get u correctly could u plz explain with example? :)
The end result is the program will ask: "Enter a month: ". So let's say the user types in "June" or "Jun" or even "Jun." it will search the list and if it finds any accepted string from the list it will then display to the user: "June has 30 days in it."
@DragonflyCode, Yes its possible , I'll update my answer soon :)
@DragonflyCode, I updated my answer, kindly noticed that i had added days in month to each of list at first index and also notice the if (mData.Any(x => x.Contains(userMonth))) condition.
|

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.