3

I'm trying to write a little C# program that reads from a text file and lets you choose a line to print out.

For some reason, it will only print lines 1,3,5,etc.

If I change the bit that says int chooseLine = Convert.ToInt32(input); to int chooseLine = (int)Convert.ToInt64(input);, then it only prints even lines.(0,2,4,6,etc).

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
class Steve
{
    public static int count = 0;
    public static String[] steveTalk;
    public static void Main()
    {
        using (StreamReader r = new StreamReader("Steve.txt"))
        {
            string line;
            while ((line = r.ReadLine()) != null)
            {
                count++;
            }
        }
        using (StreamReader sr = new StreamReader("Steve.txt"))
        {
            int i = 0;
            steveTalk = new String[count];
            String line;
            while ((line = sr.ReadLine()) != null)
            {
                steveTalk[i] = line;
                Console.WriteLine(steveTalk[i]);
                i++;
            }
        }
        while (true)
        {
            string input = Console.ReadLine();
            int chooseLine = Convert.ToInt32(input);
            try
            {
                Console.WriteLine(steveTalk[chooseLine]);
            }
            catch
            {
                Console.WriteLine("Error! Not a number or array index out of bounds");
            }
            Console.ReadLine();
        }
    }
}

Any ideas?

8
  • 3
    You can paste code and then highlight it and click on the {} button in the editor. :) Dec 5, 2011 at 3:05
  • Thanks! :) I'll do that from now on.
    – Ari Lotter
    Dec 5, 2011 at 3:07
  • @Louis you mean like public override void Close(); ? I'll do that :)
    – Ari Lotter
    Dec 5, 2011 at 3:11
  • Try writing out what chooseLine is after you convert it from a string. Narrow the source of your error. Dec 5, 2011 at 3:11
  • @aerobit no, I made a mistake. I think using does closes the stream automatically. Dec 5, 2011 at 3:12

2 Answers 2

5

I'd like to suggest the System.IO.File.ReadAllLines(filename) method.

string []lines=System.IO.File.ReadAllLines("Steve.txt");

string input ;

while ((input = Console.ReadLine()) != "end")
    {
        int chooseLine;
        int.TryParse(input,out chooseLine);
        if(chooseLine<lines.Length)
         {
           Console.WriteLine(lines[chooseLine]);
         }
    }
0
2

There is no such problem with your code. What you might experience is that you have a Console.ReadLine() at the end of your loop, so if you enter a number it will show that line, then enter another number, that number will be ignored. Every other number that you enter will be ignored, which fits your description if you only tried to enter the numbers in sequence.

Here is some improvements to the code.

  • Use File.ReadAlLines to read the file.
  • Don't use exceptions unless you need it. You can easily check the input before any exception occurs.

Code:

using System;
using System.IO;

class Steve {

  public static void Main() {
    string[] lines = File.ReadAllLines("Steve.txt");
    while (true) {
      int line;
      if (Int32.TryParse(Console.ReadLine(), out line)) {
        if (line >= 0 && line < lines.Length) {
          Console.WriteLine(lines[chooseLine]);
        } else {
          Console.WriteLine("Error! Array index out of bounds");
        }
      } else {
        Console.WriteLine("Error! Not a number");
      }
    }
  }

}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.