调试格式代码

时间:2010-06-09 19:37:41

标签: c# winforms

我正在尝试调试我的代码:

 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);
                     }
                 }
    }

它应该做的是检查列表中的每一行。如果其中一个格式不正确,则打破循环并显示一个消息框,如果所有行格式正确,则应调用WriteToFile方法。但是,当使用混合输入时对其进行测试时,它会在显示错误消息并打破循环之前调用WriteToFile方法。有人弄明白为什么?其中有一些代表点:)

提前致谢

示例:

格式正确:

G20:49:02:10 JG07
G37:84:73:20 JG48

格式不正确:

G47:29:js:20 JG29

如果用户输入

G20:49:02:10 JG07
G47:29:js:20 JG29
G37:84:73:20 JG48

然后代码应该测试第一行,看它是否格式正确,继续到第二行,看到格式不正确并打破循环并显示错误消息。

答案

   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);
                 }
    }

谢谢mmyers

2 个答案:

答案 0 :(得分:1)

您的正则表达式中似乎缺少右括号:“G [0-9] {2}:[0-9] {2}:[0-9] {2}:[0-9] {2} JG [0-9] {2“。

答案 1 :(得分:1)

此代码更好:

        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);
            }
        }