11

I have an unknown number of lines of input. I know that each line is an integer, and I need to make an array with all the lines, for example:

Input:

12
1
3
4
5

and I need to get it as an array: {12,1,3,4,5}

I have the below code, but I can't get all the lines, and I can't debug the code because I need to send it to test it.

List<int> input = new List<int>();

string line;
while ((line = Console.ReadLine()) != null) {
     input.Add(int.Parse(Console.In.ReadLine()));
}

StockItem[] stock = new StockItem[input.Count];
for (int i = 0; i < stock.Length; i++) {
    stock[i] = new StockItem(input.ElementAt(i));
}
3
  • 1
    Give Linqpad a try, you can compile and pseudo-debug easily without VS. linqpad.net
    – Mike Atlas
    Apr 29, 2012 at 22:11
  • I'm sorry i didn't see it, my spell checker is setted to Spanish and all my text is with the red marker, sorry about that.
    – Santanor
    Apr 29, 2012 at 22:12
  • 1
    Ideone is also good if you need to provide input.
    – Ry-
    Apr 29, 2012 at 22:15

3 Answers 3

18
List<int> input = new List<int>();

// As long as there are nonempty items read the input
// then add the item to the list    
string line;
while ((line = Console.ReadLine()) != null && line != "") {
     input.Add(int.Parse(line));
}

// To access the list elements simply use the operator [], instead of ElementAt:
StockItem[] stock = new StockItem[input.Count];
for (int i = 0; i < stock.Length; i++) {
    stock[i] = new StockItem(input[i]);
}
6
  • 9
    while(!string.IsNullOrEmpty(line = Console.ReadLine())) would look nicer :)
    – Ry-
    Apr 29, 2012 at 22:19
  • @minitech, Yes, at first I thought write it in the way you wrote, But after that I thought may be is a little confusing. Apr 29, 2012 at 22:21
  • 1
    The problem isn't the sintaxis, i'm not starting with C# but i don't know why this solutions doesn't work (no, your solution didn't work either) :( i'm going to continue looking for it, thank you!!
    – Santanor
    Apr 29, 2012 at 22:25
  • 1
    @SaeedAmiri ok, i trust you, and i compile it in my head and it should works, the problem must be mine... thank you for your solution, i`m using it :)
    – Santanor
    Apr 29, 2012 at 22:31
  • @Santanor, See IDEOne sample. difference in using && line != "" actually when your input is String.Empty without this checking you will get an exception. Apr 29, 2012 at 22:42
2

Do you actually need the ids in an array? I would probably try something like this:

    // Use a function that takes a StringReader as an input.
    // That way you can supply test data without using the Console class.
    static StockItem[] ReadItems(StringReader input)
    {
      var stock = new List<StockItem>();

      // Only call ReadLine once per iteration of the loop.
      // I expect this is why you're not getting all the data.
      string line = input.ReadLine();
      while( ! string.IsNullOrEmpty(line) ) {

        int id;
        // Use int.TryParse so you can deal with bad data.
        if( int.TryParse(line, out id) ) { 
          stock.Add(new Stock(id));
        }

        line = input.ReadLine();
      }

      // No need to build an populate an array yourself. 
      // There's a linq function for that.
      return stock.ToArray();
    }

Then you can call it with

  var stock = ReadItems(Console.In);
0

Using a list is a good approach. But you should think about limiting the total number of rows.

Besides, the OP did not say, that empty lines should terminate the list!

So one should check for null only.

See How to detect EOF on the console in C#? What does Console.ReadLine() returns upon EOF?

Your Answer

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

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