tags:

views:

46

answers:

3

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

+1  A: 

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".

Anna Lear
Your right i was. i'll edit that now. However, even after correcting that it still doesn't work. it seems to run the WriteToFile method BEFORE the formatting test.
Arcadian
A: 

What is the value of txtInput.Text (for your test)?

Patrice Pezillier
see my edit for an example of what the txtInput textbox would contain
Arcadian
+1  A: 

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);
            }
        }
Patrice Pezillier
yh mmyers thought of using a boolean flag too.but you have to move the messagebox to an "else" part of the boolean if test. otherwise it wont work properly. see my edit.some rep for you though, for getting it right.
Arcadian