【问题标题】:I get an error with trying to parse the integer from the textfile [closed]尝试解析文本文件中的整数时出现错误[关闭]
【发布时间】:2015-04-27 12:01:41
【问题描述】:

我在尝试“登录”时遇到错误。这是一个学校项目,我知道它很简单,我只需要它正常运行,感谢任何帮助。尝试忽略明显低效的代码。

   class UserVariables
        {
            public static int avatarC;
            public static int colourC;
            public static string userNameC;
            public static string passWordC;
            public static int highScoreC;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                bool success = false;

                string UsernameTXT = "userName.txt";
                string PasswordTXT = "userPass.txt";
                string ColourTXT = "userColour.txt";
                string AvatarTXT = "userAvatar.txt";
                string ScoreTXT = "userScore.txt";

                using (StreamReader user_Login = new StreamReader(UsernameTXT))
                {
                    using (StreamReader pass_Login = new StreamReader(PasswordTXT))
                    {
                        using (StreamReader colour_Login = new StreamReader(ColourTXT))
                        {
                            using (StreamReader avatar_Login = new StreamReader(AvatarTXT))
                            {
                                using (StreamReader score_login = new StreamReader(ScoreTXT))
                                {
                                    var lineCount = File.ReadLines(UsernameTXT).Count();
                                    string userRead;
                                    string passRead;
                                    string colourRead;
                                    string avatarRead;
                                    string scoreRead;

                                    for (int a = 0; a < lineCount; a++)
                                    {
                                        userRead = user_Login.ReadLine();
                                        passRead = pass_Login.ReadLine();
                                        colourRead = user_Login.ReadLine();
                                        avatarRead = pass_Login.ReadLine();
                                        scoreRead = user_Login.ReadLine();

                                        if ((textBox1.Text == userRead) && (textBox2.Text == passRead))
                                        {
                                            UserVariables.userNameC = textBox1.Text;
                                            UserVariables.passWordC = textBox2.Text;

                                            UserVariables.colourC = int.Parse(colourRead);
                                            // Theres a problem with this line.

                                            UserVariables.avatarC = int.Parse(avatarRead);
                                            UserVariables.highScoreC = int.Parse(scoreRead);
                                            MessageBox.Show("Login successful!",
                                                "Success");

                                            success = true;

                                            new Menus().Show();

                                            this.Hide();

                                            MessageBox.Show("Hi, " + UserVariables.userNameC + "!");
                                            break;
                                        }
                                    }

                                    if (success == false)
                                    {
                                        label1.Show();

                                        MessageBox.Show("Login was unsuccessful.",
                                            "Error");
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

【问题讨论】:

  • 您需要描述错误...您尝试解析的字符串的内容是什么?
  • “输入字符串的格式不正确”但我确定它是一个数字,简单的数字就像 2 或 3
  • 那个错误表明,它到底是什么样子?
  • 在有问题的行设置断点并在调试器中检查值。
  • 好吧,我使用了一个 try catch 来解决它,但它只是一个巨大的错误,上面写着“输入字符串的格式不正确”,然后它开始胡言乱语,比如 intParse decimalboolean 和类似的东西

标签: c# text-files system.io.file


【解决方案1】:

错误是“输入字符串的格式不正确”,它告诉我 colourRead 的至少一个值不是整数值。 int.Parse 忽略空格,因此它不是格式问题。它不能是简单的整数值,必须包含另一个字符,如“2a”、“2.0”、“1,000”等。

要准确确定哪个值,您可以创建断点或监视并开始调试,也可以使用 int.TryParse

试试这个

if(!int.TryParse(colourRead, out UserVariables.colourC))
{
    MessageBox.Show(colourRead);
}
// Theres a problem with this line.

它可能会弹出一个窗口,告诉您它试图将什么值转换为 int。


另外,我预见到您阅读文件的方式会出现问题。这样做可以得到 userTXT 中的行数:

 var lineCount = File.ReadLines(UsernameTXT).Count();

它将读取整个文件内容,并计算存在多少行。这不是一个很好的方法,但只要文件不太长,它就应该可以正常工作。

然后您将单步执行此文件中的每一行,但在 for 循环的每次迭代中读取 3 行

userRead = user_Login.ReadLine();
...
colourRead = user_Login.ReadLine();
...
scoreRead = user_Login.ReadLine();

在 for 循环完成之前,您将用完从文件中读取的行数。

【讨论】:

  • 您的意思可能是int.TryParse()(这需要out 参数);我很确定int.Parse() 会抛出一个无法解析的值。这当然是问题所在。
  • 是的,我的意思是 int.TryParse。现在修复它。
猜你喜欢
  • 2016-07-07
  • 1970-01-01
  • 2013-05-31
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
  • 2018-05-01
  • 2019-07-31
  • 1970-01-01
相关资源
最近更新 更多