Reputation: 25
I've written code that should not accept incorrect format of the date. Mine displays invalid choice but stores the incorrect format of the date in the text file. If the date is in incorrect format, it should not be stored in the text file.
using System;
using System.Collections;
using System.IO;
using System.Globalization;
class FunWithScheduling
{
public void AddView()
{
FileStream s = new FileStream("Scheduler.txt",FileMode.Append,FileAccess.Write);
StreamWriter w = new StreamWriter(s);
Console.WriteLine("Enter the Name of the Person To Be Met:");
string Name = Console.ReadLine();
Console.WriteLine("Enter the Date Scheduled For the Meeting:");
string Date = Console.ReadLine();
DateTime date;
if(!DateTime.TryParseExact(Date,"MM-dd-yyyy",new CultureInfo("en-US"),DateTimeStyles.None,out date))
{
Console.WriteLine("Invalid Choice");
}
Console.WriteLine("Enter the Time Scheduled For the Meeting:");
string Time = Console.ReadLine();
string line = Name + " "+ Date +" " + Time;
w.WriteLine(line);
w.Flush();
w.Close();
s.Close();
}
static void Main()
{
FunWithScheduling a = new FunWithScheduling();
a.AddView();
}
}
This modified program doesn't work. While is never ending and the correct format of the date is not accepted nor saved in the text file.
I am not allowed to use the string builder.
using System;
using System.Collections;
using System.IO;
using System.Globalization;
class FunWithScheduling
{
public void AddView()
{
FileStream s = new FileStream("Scheduler.txt",FileMode.Append,FileAccess.Write);
StreamWriter w = new StreamWriter(s);
Console.WriteLine("Enter the Name of the Person To Be Met:");
string Name = Console.ReadLine();
Console.WriteLine("Enter the Date Scheduled For the Meeting:");
string Date = Console.ReadLine();
DateTime date;
if(!DateTime.TryParseExact(Date,"MM-dd-yyyy",new CultureInfo("en-US"),DateTimeStyles.None,out date))
{
Console.WriteLine("Invalid date format!");
while(!DateTime.TryParseExact(Date,"MM-dd-yyyy",new CultureInfo("en-US"),DateTimeStyles.None,out date))
{
Console.WriteLine("Invalid Date Entered, please format MM-dd-yyyy");
Date = Console.ReadLine();
}
}
Console.WriteLine("Enter the Time Scheduled For the Meeting:");
string Time = Console.ReadLine();
string line = Name + " "+ Date +" " + Time;
w.WriteLine(line);
w.Flush();
w.Close();
s.Close();
}
static void Main()
{
FunWithScheduling a = new FunWithScheduling();
a.AddView();
}
}
Upvotes: 0
Views: 538
Reputation: 1069
You have 2 options when the user enter the wrong date:
To close the stream and exit out you must add below code after:
Console.WriteLine("Invalid Choice");
w.Flush();
w.Close();
s.Close();
return;
And
To ask the user to re-enter, until he specifies the correct answer, add below code after:
Console.WriteLine("Invalid Choice");
while(!DateTime.TryParseExact(Date, "MM-dd-yyyy", new CultureInfo("en-US"), DateTimeStyles.None, out date))
{
Console.WriteLine("Invalid Date Entered, please format MM-dd-yyyy");
Date = Console.ReadLine();
}
Upvotes: 0
Reputation: 11
First of all, yes, use a StringBuilder instead of just + with string, because when you do that, string will multiply the memory usage of both of the strings, which means you have 2 memory allocations for 2 separated strings, and 1 for the con-catted one, just a tip for future.
All you need to do is either do
StringBuilder sb = new StringBuilder();
sb.Append(Name);
if(!DateTime.TryParseExact(Date,"MM-dd-yyyy",new CultureInfo("en-US"),DateTimeStyles.None,out date))
{
Console.WriteLine("Invalid Choice");
}
else
{
sb.Append(Date);
}
and then when you write to the file itself just do
w.WriteLine(sb.ToString());
Upvotes: 0
Reputation: 119
// Make Date as blank when entered date is invalid; it will not be stored in the text file.
if(!DateTime.TryParseExact(Date,"MM-dd-yyyy",
new CultureInfo("en-US"),DateTimeStyles.None,out date))
{
Console.WriteLine("Invalid Choice");
Date = "";
}
Upvotes: 0
Reputation: 47377
I suggest you use an "else" statement to fix issue of "Date". And then use a String Builder instead of +
and build the string.
I've tried to help you here, but I haven't run it through a compiler.
using System;
using System.Collections;
using System.IO;
using System.Globalization;
using System.Text;
class FunWithScheduling
{
public void AddView()
{
FileStream s = new FileStream("Scheduler.txt",FileMode.Append,FileAccess.Write);
StreamWriter w = new StreamWriter(s);
var builder = new StringBuilder();
Console.WriteLine("Enter the Name of the Person To Be Met:");
string Name = Console.ReadLine();
builder.Append(Name);
Console.WriteLine("Enter the Date Scheduled For the Meeting:");
string Date = Console.ReadLine();
DateTime date;
if(!DateTime.TryParseExact(Date,"MM-dd-yyyy",new CultureInfo("en-US"),DateTimeStyles.None,out date))
{
Console.WriteLine("Invalid Choice");
} else {
builder.Append(date.ToString("MMMM dd, yyyy"));
}
Console.WriteLine("Enter the Time Scheduled For the Meeting:");
string Time = Console.ReadLine();
builder.Append(Time);
w.WriteLine(builder.ToString());
w.Flush();
w.Close();
s.Close();
}
static void Main()
{
FunWithScheduling a = new FunWithScheduling();
a.AddView();
}
}
You could also try a while statement similar to this in order to force the user to input the right date.
while(!DateTime.TryParseExact(Date,"MM-dd-yyyy",new CultureInfo("en-US"),DateTimeStyles.None,out date)){
Console.WriteLine("Invalid Date Entered, please format MM-dd-yyyy");
Date = Console.ReadLine();
}
Upvotes: 0
Reputation: 2816
You have to add:
return;
after :
Console.WriteLine("Invalid Choice");
Also better to move FileStream
and StreamWriter
initialization till after the condition check:
using System;
using System.Collections;
using System.IO;
using System.Globalization;
class FunWithScheduling
{
public void AddView()
{
Console.WriteLine("Enter the Name of the Person To Be Met:");
string Name = Console.ReadLine();
Console.WriteLine("Enter the Date Scheduled For the Meeting:");
string Date = Console.ReadLine();
DateTime date;
if(!DateTime.TryParseExact(Date,"MM-dd-yyyy",new CultureInfo("en-US"),DateTimeStyles.None,out date))
{
Console.WriteLine("Invalid Choice");
return;
}
Console.WriteLine("Enter the Time Scheduled For the Meeting:");
string Time = Console.ReadLine();
string line = Name + " "+ Date +" " + Time;
FileStream s = new FileStream("Scheduler.txt",FileMode.Append,FileAccess.Write);
StreamWriter w = new StreamWriter(s);
w.WriteLine(line);
w.Flush();
w.Close();
s.Close();
}
static void Main()
{
FunWithScheduling a = new FunWithScheduling();
a.AddView();
}
}
Upvotes: 3