I.Ali
I.Ali

Reputation: 5

c# how to print string array in the console?

The bold text is where i need to change something but not sure what. I want the console to display the month as a string e.g 'Jul' 'Aug'... basically my coding for case 1 displays = 1/1/2001 and case 3 i want it to display = 1/Jan/2001

static void Main(string[] args)
{

    do
    {
        Console.WriteLine("Please select a date format by entering either the 1, 2 or 3 Key");
        Console.WriteLine(" 1 = dd/mm/yyyy ");
        Console.WriteLine(" 2 = mm/dd/yyyy ");
        Console.WriteLine(" 3 = dd/mmm/yyyy ");
    } while (!int.TryParse(Console.ReadLine(), out f) || f < 0 || f>3);

    switch(f)
    {
        case 1:
            getDate1();
            Console.WriteLine("The day after {0}/{1}/{2} is {3}", d, m, y, md.NextDay1());
            break;
        case 2:
            getAmericanDate();
            Console.WriteLine("The day after {1}/{0}/{2} is {3}", d, m, y, md.NextDay1());
            break;
        **case 3:
            getAbbreviatedMonth();
            Console.WriteLine("The day after {1}/{4}/{2} is {3}", d, m, y, md.NextDay1());
            break;**

    }


static void getAbbreviatedMonth() //dd/mmm/yyyy
{

    do
    {
        Console.Write("PLease enter the year (not earlier than 1812) as 4 digits  >> ");
    } while (!int.TryParse(Console.ReadLine(), out y) || y < 1812);


    do
    {
        Console.Write("Please enter the month as a three letter character ( e.g 'Jul')   >> ");
    } while (isCorrectMonth(Console.ReadLine()));


    do
    {

        Console.Write("Please enter the day as a whole between 1 & {0} >> ", DayInMonth(m, y));
    } while (!int.TryParse(Console.ReadLine(), out d) || d > DayInMonth(m, y) || d < 1);



    md = new myDate(d, m, y);
}


static bool isCorrectMonth(string monthToCheck)
{
    string stringToCheck = monthToCheck.ToLower();
    string[] stringArray = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" };
    foreach (string x in stringArray)
    {
        if (x.Contains(stringToCheck))
        {
            // Process..
            return true;
        }
        else
        {
            return false;
        }
    }
    return false;
}

Upvotes: 1

Views: 724

Answers (2)

berserck
berserck

Reputation: 500

See this example to format the date:

DateTime time = DateTime.Now;             // Use current time.
string format = "MMM ddd d HH:mm yyyy";   // Use this format.
Console.WriteLine(time.ToString(format)); // Write to console.

Output is:

Feb Fri 27 11:41 2009

Source is http://www.dotnetperls.com/datetime-format

Upvotes: 0

sschimmel
sschimmel

Reputation: 196

Console.WriteLine("The day after {1}/{4}/{2} is {3}", d, m, y, md.NextDay1())

Your arguments here should range from 0-3, not 1-4.

You might find it easier to parse your input into a DateTime, which could then easily be output in a variety of formats -- and you could find the next day simply by calling .AddDays(1).

Upvotes: 1

Related Questions