【问题标题】:input string was not in correct format error输入字符串格式不正确错误
【发布时间】:2012-09-03 10:14:28
【问题描述】:

我正在尝试在 21 天内使用 Sams Teach Yourself C# 学习 C# 的基础知识。

我通过从第 1 天的类型和运行部分逐行复制它来创建这个程序。它编译得很好,但是当你运行它时,它给出了这个错误:“输入字符串的格式不正确”。

我正在从控制台运行程序。

我正在使用 Visual Studio 2010 Express 编辑器。

我复制的代码是:

using System;
using System.IO;

/// <summary>
/// Class to number a listing. Assumes fewer than 1000 lines.
/// </summary>

class NumberIT
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>

    public static void Main(string[] args)
    {
        // check to see if a file name was included on the command line.

        if (args.Length <= 0)
        {
            Console.WriteLine("\nYou need to include a filename.");
        }
        else
        {
            // declare objects for connecting to files...
            StreamReader InFile = null;
            StreamWriter OutFile = null;

            try
            {
                // Open file name included on command line...
                InFile = File.OpenText(args[0]);

                // Create the output file...
                OutFile = File.CreateText("outfile.txt");
                Console.Write("\nNumbering...");

                // Read first line of the file...
                string line = InFile.ReadLine();
                int ctr = 1;

                // loop through the file as long as not at the end...
                while (line != null)
                {
                    OutFile.WriteLine("{1}: {2}", ctr.ToString().PadLeft(3, '1'), line);
                    Console.Write("..{1]..", ctr.ToString());
                    ctr++;
                    line = InFile.ReadLine();
                }
            }

            catch (System.IO.FileNotFoundException)
            {
                Console.WriteLine("Could not find the file {0}", args[0]);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: {0}", e.Message);
            }
            finally
            {
                if (InFile != null)
                {
                    // Close the files
                    InFile.Close();
                    OutFile.Close();
                    Console.WriteLine("...Done.");
                }
            }
        }
    }
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    你的罪魁祸首是 OutFile.WriteLine 和 Console.Write 语句:

    OutFile.WriteLine("{1}: {2}", ctr.ToString().PadLeft(3, '1'), line);
    Console.Write("..{1]..", ctr.ToString());
    

    应该是:

    OutFile.WriteLine("{0}: {1}", ctr.ToString().PadLeft(3, '1'), line);
    Console.Write("..{0}..", ctr.ToString());
    

    请注意,格式字符串中的占位符从 0 开始。第二个语句中的右括号是方括号而不是大括号。

    另一个提示:在后一种情况下,您无需在 ctr 上调用 .ToString(),除非您想明确指定文化。

    【讨论】:

    • 如果要链化一个String方法,需要调用ToString(),比如PadLeft()这里在WriteLine()语句中;但在Write() 语句中确实不需要它。
    • @Gorpik 是的,我指的是 Console.Write 语句。我承认我本来可以更清楚一点。
    【解决方案2】:

    注意事项:

    OutFile.WriteLine("{1}: {2}", ctr.ToString().PadLeft(3, '1'), line);
    

    索引是从0开始的,所以应该是OutFile.WriteLine("{0}: {1}"...

    Console.Write("..{1]..", ctr.ToString());
    

    这里有一个错字! (我希望!),它应该是 0 而不是 1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-19
      • 2018-04-07
      • 1970-01-01
      相关资源
      最近更新 更多