none
reading from a file and using split function , it give me index out of range while taking this value and putting it into the list . RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;

    namespace Map_Routing
    {
        class vertex
        {
            public int vertex_number;
            public float x;
            public float y;
            public List<edge> edge_list;

            public vertex(int v, float p1, float p2, List<edge> l)
            {
                vertex_number = v;
                x = p1;
                y = p2;
                edge_list = l;

            }
        }
        class edge
        {
            // edge between vertex v1 and vertex v2
            public int v1;
            public int v2;
            public float road_len;
            public int road_speed;
            public edge(int var1, int var2, float var3, int var4)
            {
                v1 = var1;
                v2 = var2;
                road_len = var3;
                road_speed = var4;
            }
        }

        class Program
        {

            static void Main(string[] args)
            {
                
                List<vertex> graph = new List<vertex>();
                
                StreamReader sr = new StreamReader("../../map1.txt");

                string  numOf_Vertex = sr.ReadLine();

                string line = File.ReadLines("../../map1.txt").Skip(1).Take(1).First();

                int count = int.Parse(numOf_Vertex);
                while (count != 0)
                {
                    line = sr.ReadLine();

                   
                    string[] values = line.Split(' ');
                   // float s;                             
                  //  s = float.Parse(values[2]); // it works here and print the correct values 
                  //  Console.WriteLine(s);         

                       for(int i=0; i < int.Parse(numOf_Vertex);i++)
                        {

                        graph[0].vertex_number= int.Parse(values[0]); // it gives an exception here " index out of range" 
                        graph[0].x = float.Parse(values[1]);
                        graph[0].y = float.Parse(values[2]);

                       }
                    count--;

                }
                while(count != 0)
                {
                    for(int i=0; i<count; i++)
                    {
                        Console.WriteLine(graph[i].vertex_number);
                        Console.WriteLine(" ");
                        Console.WriteLine(graph[i].x);
                        Console.WriteLine(" ");
                        Console.WriteLine(graph[i].y);
                        Console.WriteLine(Environment.NewLine);
                    }
                    count--;
                }
                




            }
        }
    }
    Tuesday, December 11, 2018 12:15 AM

All replies

  • Greetings Mary.

    You need to add a vertex to the graph list. It is graph[0] that is out of range, because graph is empty.

    Tuesday, December 11, 2018 12:24 AM
  • Hi Mary Mikhail,

    Thank you for posting here.

    How many lines in your map1.txt file?

    Based on my test, there are many effects cause the error. Could you provide your map1.txt file and the details of your error message for us to test?

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, December 11, 2018 6:23 AM
    Moderator
  • Hi Mary Mikhail,

    As Ante Meridian mentioned, you create a collection of vertex, but does not assign related value into it, and it is empty. it will throw the excepiton "index out of range" because the collection does not have graph[0] object in it.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 17, 2018 5:15 AM
    Moderator