-2

I'm getting an error IndexOutOfRangeException was unhandled at the line int euros = int.Parse(values[1]).

My .csv file looks:

name,   1,  2
name1,  3,  4
name2,  5,  6

 public static void ReadData(out Turistai[] tourists, out int amount)
    {
        amount = 0;
        tourists = new Turistai[MaxTourists];
        using (StreamReader reader = new StreamReader("C:\\Users\\Andrius\\Desktop\\Mokslams\\C#\\Pratybos\\P2\\P2.1\\turistai.csv"))
        {
            string line = null;
            while( (line = reader.ReadLine()) != null) 
            {
                string[] values = line.Split(';');

                string name = values[0];
                int euros = int.Parse(values[1]);
                int cents = int.Parse(values[2]);
                Console.WriteLine(euros);
                //Turistai tourists = new Turistai(name, euros, cents);
                amount++;
            }
        }
    }
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
Andrius
  • 21
  • 2
  • 5
  • Hi Andrius. I've cast a vote to to close your post. Please see the How to Ask page for info on the kinds of questions that are useful here. Posts that are basically code-dumps followed by an implicit or explicit "Please solve this for me" are frowned upon. You should use a debugger to step through the code instead. – code_dredd Sep 13 '16 at 08:08
  • as @ray said: use the debugger, set the cursor to `string[] values = line.split(';');`, hit F9 and go F5. Then you can step through it. move your cursor over `values` and view the content. – Radinator Sep 13 '16 at 08:45

2 Answers2

0

Your CSV input is comma separated while in the code you're splitting by semicolons. Change the split() parameter to ,:

string[] values = line.Split(',');

You may also want to add input format check to ensure the values array contains at least three items and the numeric fields do actually contain integer values (int.TryParse() may help with this):

while( (line = reader.ReadLine()) != null) 
{
    string[] values = line.Split(',');
    if (values.Length < 3)
    {
        Console.Error.WriteLine("Invalid input line: " + line);
        continue;
    }

    string name = values[0];
    int euros;
    if (!int.TryParse(values[1], out euros))
    {
        Console.Error.WriteLine("Invalid euros value in the line: " + line);
        continue;
    }
    int cents;
    if (!int.TryParse(values[2], out cents))
    {
        Console.Error.WriteLine("Invalid cents value in the line: " + line);
        continue;
    }
    Console.WriteLine(euros);
    //Turistai tourists = new Turistai(name, euros, cents);
    amount++;
}
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
  • Sorry, I just accidentally put a wrong symbol, however the problem remains the same- i get the same error. EDIT: actually right now, it's says "FormatException was unhandled" - Input string form was not in correct format. – Andrius Sep 13 '16 at 07:58
  • Then most likely you have some incorrectly formatted lines in your CSV file. Updated the answer with such check. – Dmitry Egorov Sep 13 '16 at 08:07
  • "FormatException was unhandled" is because of incorrect numeric values. Use `int.TryParse()` to check that. Please see the updated answer. – Dmitry Egorov Sep 13 '16 at 11:09
0

Probably, you have some empty lines in the CSV file. I suggest using Linq to sum up the euros:

var data = File
  .ReadLines("C:\\Users\\Andrius\\Desktop\\Mokslams\\C#\\Pratybos\\P2\\P2.1\\turistai.csv")
  .Select(line => line.Split(','))
  .Where(items => items.Length >= 2) // filter out empty/incomplete lines
  // To debug, let's take euros only
  .Select(items => int.Parse(items[1]));
  // In the final solution we'll create Touristai instances
  // .Select(items => new Touristai(items[0], int.Parse(items[1]), int.Parse(items[2])))
  .ToArray();

Console.WriteLine(String.Join(Environment.NewLine, data));

Console.WriteLine(data.Sum());

Final solution will be

public static void ReadData(out Turistai[] tourists, out int amount) {
  tourists = File
    .ReadLines(@"C:\Users\Andrius\Desktop\Mokslams\C#\Pratybos\P2\P2.1\turistai.csv")
    .Select(line => line.Split(','))
    .Where(items => items.Length >= 2) // filter out empty/incomplete lines
    .Select(items => new Touristai(items[0], int.Parse(items[1]), int.Parse(items[2])))
    .ToArray();

  //TODO: check syntax (I've sugested Touristai should have Euro property) 
  amount = tourists.Sum(tourist => tourist.Euro); 
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215