I suggest you...

Reading from a file with using...gives dumb error message

Please explain to me how the following code could possibly get an error message:

Here are the instructions:

Read from the local file 'Movies.txt'

Create a file stream caleld file with a using statement.
Create a stream reader called reader using the file stream. This block must be nested using block syntax { ... }.
While we are not at the end of the stream, create a local string declaration named movie and assign this the value of the next line from the file.
Write each move to the Console using WriteLine.

Here's the code:

using System;
using System.IO;

public class Program
{
public static void Main()
{
File.WriteAllText(@".\Movies.txt", "Star Wars\nThe Empire Strikes Back\nReturn Of The Jedi\n");

// Your code here.

using (var file = new FileStream(@".\Movies.txt", FileMode.Open))

using (var reader = new StreamReader(file))
{
while (!reader.EndOfStream)
{
string movie = reader.ReadLine();
Console.WriteLine(movie);
}
}


}
}

Here's the results:

Star Wars
The Empire Strikes Back
Return Of The Jedi

Here's the error message:

Not all requirements have been met.

You must nest your second using statement within a block statement using the { and } enclosing tokens.

What the hell? I DID enclose the second using statement within a block statement using { and}. Why is it wrong?

1 vote
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    I agree to the terms of service
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Anonymous shared this idea  ·   ·  Admin →

    1 comment

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      I agree to the terms of service
      Signed in as (Sign out)
      Submitting...
      • Anonymous commented  · 

        it looks like you used the stacked nesting... which is perfectly valid code, it just wants you to actually nest them, the next exercise uses the syntax you have. The code below has the syntax the exercise is expecting.

        using System;
        using System.IO;

        public class Program
        {
        public static void Main()
        {
        File.WriteAllText(@".\Movies.txt", "Star Wars\nThe Empire Strikes Back\nReturn Of The Jedi\n");

        // Your code here.

        using (var file = new FileStream(@".\Movies.txt", FileMode.Open))
        { using (var reader = new StreamReader(file))
        {
        while (!reader.EndOfStream)
        {
        string movie = reader.ReadLine();
        Console.WriteLine(movie);
        }
        }
        }
        }
        }

      Feedback and Knowledge Base