Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

input string was not in correct format error

Tags:

c#

I am trying to learn the basics of C# using Sams Teach Yourself C# in 21 days.

I have created this program by copying it line by line from the day 1 type and run section. It compiles fine but when you run it, it gives this error: "Input string was not in the correct format".

I am running the program from the console.

I am using the Visual Studio 2010 Express editor.

The code I copied is:

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.");
                }
            }
        }
    }
}
like image 466
digikam Avatar asked Dec 18 '25 03:12

digikam


1 Answers

Your culprits here are the OutFile.WriteLine and Console.Write statements:

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

It should read:

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

Note that the placeholders in the format string start from 0. Your closing bracket on the second statement was a square bracket instead of a curly one.

Another tip: you don't need to call .ToString() on ctr in the latter case, unless you wanna specify a culture explicitly.

like image 111
theiterator Avatar answered Dec 20 '25 00:12

theiterator



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!