3

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

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

You need to call ToString() if you want to chain a String method, such as PadLeft() here in the WriteLine() statement; but it is true that you don't need it in the Write() statement.
@Gorpik yeah, I was referring to the Console.Write statement. I admit I could've been a bit clearer on that one.
0

Few things to note:

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

the index is 0 based, so it should be OutFile.WriteLine("{0}: {1}"...

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

there is a typo here! (I hope!), and again it should be 0 not 1.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.