Debug formatting code
I'm trying to debug my code here:
private void CheckFormatting()
{
StringReader objReaderf = new StringReader(txtInput.Text);
List<String> formatTextList = new List<String>();
do
{
formatTextList.Add(objReaderf.ReadLine());
}
while (objReaderf.Peek() != -1);
objReaderf.Close();
for (int i = 0; i < formatTextList.Count; i++)
{
if (!Regex.IsMatch(formatTextList[i],
"G[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2} JG[0-9]{2}"))
{
MessageBox.Show("开发者_开发技巧Line " + formatTextList[i] + " is not formatted correctly.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
break;
}
else
{
this.WriteToFile();
MessageBox.Show("Your entries have been saved.", "Saved",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
what it is supposed to do is to check each line in the list. if one of them isn't formatted correctly, then break the loop and display a message box, if all the lines are formatted properly then it should call the WriteToFile method. However, when testing it using a mix of input that was and was not correctly formatted it called the WriteToFile method before it displayed the error message and broke the loop. Anyone figure out why? There's some rep points in it for you :)
Thanks in advance
Examples:
These are correctly formatted:
G20:49:02:10 JG07
G37:84:73:20 JG48
This is not correctly formatted:
G47:29:js:20 JG29
If the user inputs
G20:49:02:10 JG07
G47:29:js:20 JG29
G37:84:73:20 JG48
then the code should test the first line, see that it is correctly formatted, move on to the second line, see that is not formatted properly and break the loop and display the error message.
Answer
private void CheckFormatting()
{
StringReader objReaderf = new StringReader(txtInput.Text);
List<String> formatTextList = new List<String>();
do
{
formatTextList.Add(objReaderf.ReadLine());
}
while (objReaderf.Peek() != -1);
objReaderf.Close();
bool FlagCheck = true;
for (int i = 0; i < formatTextList.Count; i++)
{
if (!Regex.IsMatch(formatTextList[i],
"G[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2} JG[0-9]{2}"))
{
FlagCheck = false;
break;
}
}
if (FlagCheck == true)
{
this.WriteToFile();
MessageBox.Show("Your entries have been saved.", "Saved",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Line " + formatTextList[i] + " is not formatted correctly.",
"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Thanks mmyers
Looks like you're missing a closing brace in your regex: "G[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2} JG[0-9]{2".
This code is better :
private void CheckFormatting ()
{
StringReader objReaderf = new StringReader (txtInput.Text);
List<String> formatTextList = new List<String> ();
do {
formatTextList.Add (objReaderf.ReadLine ());
} while (objReaderf.Peek () != -1);
objReaderf.Close ();
bool testSucceed = true;
for (int i = 0; i < formatTextList.Count; i++) {
if (!Regex.IsMatch (formatTextList[i], "G[0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{2} JG[0-9]{2}")) {
MessageBox.Show ("Line " + formatTextList[i] + " is not formatted correctly.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
testSucceed = false;
break;
}
}
if (testSucceed) {
this.WriteToFile ();
MessageBox.Show ("Your entries have been saved.", "Saved", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
精彩评论